hi i need some help,
i'm working on a search page for a client and i need to extract multiple values based on id.
| post_id | meta_key | meta_value |
| 400 | book | mysql book |
| 500 | book | php book |
| 500 | dvd | animation |
| 400 | dvd | action |
i want to extract meta_key = 'book' with the meta_value 'mysql book' and meta_key = 'dvd' with meta_value 'action'.
they all have the same 'post_id' so i thought doing some JOIN but it didn't work for me.
(meta_key = 'book' AND meta_value = 'mysql book')
OR (meta_key = 'dvd' AND meta_value = 'action')
if a user search for 'mysql book' and 'animation' it needs to return 0 results, now it returns 2 results, that's why i need to check the post_id to match the value or key, any idea?
That's not what you asked. You asked:
Please describe the process. What exactly can the user do on the search page? Choose one value? Choose multiple values?
What data do you get from the form to perform the query with? meta_key? meta_value? post_id?
guido, you forgot GROUP BY post_id HAVING COUNT(*)=2 in post #2
the post_id wants to have both attributes
'Forgot' isn't the right word... since I don't understand what the OP needs
So that could well be the solution. I have no idea.
FROM ( SELECT post_id
WHERE meta_key = 'book' AND meta_value = 'mysql book'
OR meta_key = 'dvd' AND meta_value = 'action'
HAVING COUNT(*) = 2 ) AS a
ON someOtherTable.post_id = a.post_id
i think to make it easier i just need to select meta_key and meta_value that have the same post_id. so if the user searches for dvd = 'animation' and book = 'php book' it will return no results because they don't share the same post_id.
if the user search for dvd = 'action' and book = 'mysql book' in that case it would return both values, because they share the same post_id.
i hope i explained it better, it much more difficult to me to explain it in english.
edited: @r937 just saw your query, gonna try it now.
are you doing these searches in multiple input or combo boxes?
well i managed to solve it using HAVING COUNT(*) = 2.
thanks a lot.