I need search for two values in the same field.
ââââââ¦âââââââââââââ¦âââââââââââââââ
â ID â Meta_key â Meta_value â
â âââââ¬âââââââââââââ¬âââââââââââââââ£
â 1 â first_name â pritesh â
â 2 â last_name â mahajan â
â 3 â first_name â ritesh â
â 4 â last_name â jain â
â 5 â first_name â john â
â 6 â last_name â a â
â 7 â first_name â Mambo â
â 8 â last_name â Nombo â
ââââââ©âââââââââââââ©âââââââââââââââ
This is my table and I want to search all the first names and last names. Below is my query but this does not return what I want.
SELECT *
FROM `wp_usermeta`
WHERE `meta_key` = 'last_name'
AND `meta_value` LIKE '%mahajan%'
AND `meta_key` = 'first_name'
AND `meta_value` LIKE '%a%';
Here you go
SQL Fiddle
Query:
Results:
With this, you can easily search using the two virtual columns
first_name
andlast_name
. You just need to add additional conditions to theWHERE
clause. something like this:will produce
NOTE
The big assumption I have made here (based on your sample data) is that the related
first_name
andlast_name
are always going to have successiveid
‘s. (two successive records in the table).Can you try this,
I think that both rows have to share the same key. Maybe it’s
ID
, oruser_id
(not shown in your question?). If this is the case, you could use this query to return the IDs you are looking for:please substitute ID with the actual ID.
Schema
And Query should be like this….