I’m trying to make a query but I can’t get the grips of it. I want to select all records by certain meta_keys. It works fine when i query by one meta key. But when I want to make the query by two meta_key’s it’s returning nothing. Can someone please help me?!
The query that works:
SELECT *
FROM `uszc_users` AS u, `uszc_utilizatori_acord` AS ua, `uszc_usermeta` AS ub
WHERE ua.email = u.user_email
AND ub.user_id = u.id
AND ub.meta_key = 'first_name'
AND acord = 'DA'
LIMIT 0 , 30
And the query that returns nothing:
SELECT *
FROM `uszc_users` AS u, `uszc_utilizatori_acord` AS ua, `uszc_usermeta` AS ub
WHERE ua.email = u.user_email
AND ub.user_id = u.id
AND ub.meta_key = 'first_name'
AND ub.meta_key = 'last_name'
AND acord = 'DA'
LIMIT 0 , 30
I have 3 tables, table uszc_usermeta contains first_name and last_name which are in the meta_value column both.
You can’t have any record with
That has no sense, so just that simple:
UPDATE Since the OP said that @bhelmet posted correct answer I want to clarify for OP how good query should look like imho:
You’re asking for all records where
meta_key
equals BOTH ‘first_name’ and ‘last_name’. You probably want to do something likeAND ub.meta_key IN ('first_name', 'last_name')
.To check two conditions for ‘usermeta’ you should join table twice. You need something like this: