简体   繁体   中英

Join Query on multiple rows to search in

Currently I am working on a Wordpress site, and I would like it to be possible to search for users on their first and last name on a part of my site.

Wordpress makes use of a meta table to store any 'extra' data for a user. And the first and last name are some of those meta fields.

Now I am trying to create a query which gets me a result which returns me a user matching the first and last name, the only problem is that there is a meta row for last name and another one for first-name, they both do have a reference to the same user_id. Only I don't know how I can create the query correctly.

When I have this query

SELECT U.id FROM wp_users AS U, wp_usermeta as US
WHERE U.id=US.user_id
AND (US.meta_key='first_name' AND US.meta_value='MyFirstName')

It does return the user I am looking for, however when I try this

SELECT U.id FROM wp_users AS U, wp_usermeta as US
WHERE U.id=US.user_id
AND (US.meta_key='first_name' AND US.meta_value='MyFirstName')
AND (US.meta_key='last_name' AND US.meta_value='Dijkstra')

I get no results at all, could someone help me?

In case someone is interested in the database structure, here ( http://cl.ly/6rGx ) is the users table and here ( http://cl.ly/6rts ) is the users_meta table.

Thanks in advance

You can't join one row to two other rows of another table at the same time, so you need to join the other table twice, once for first name and once for last name. Like this:

SELECT U.id 
FROM wp_users AS U
inner join wp_usermeta as UMF on U.id = UMF.user_id 
inner join wp_usermeta as UML on U.id = UML.user_id 
WHERE UMF.meta_key = 'first_name' 
    AND UMF.meta_value = 'MyFirstName'
    AND UML.meta_key = 'last_name' 
    AND UML.meta_value = 'Dijkstra'

Consider the logic by re-writing the order of the AND s (no, parens don't make a difference).

US.meta_key='first_name' AND US.meta_key='last_name' AND
US.meta_value='MyFirstName' AND US.meta_value='Dijkstra'

See the problem? This condition will never be true, because neither US.meta_key nor US.meta_value will never be two values. You need to use an OR , not and AND .

EDIT

A query like this would give you the desired results:

SELECT U.id FROM wp_users AS U, wp_usermeta as US
WHERE U.id=US.user_id AND (
    (US.meta_key='first_name' AND US.meta_value='MyFirstName')
    OR (US.meta_key='last_name' AND US.meta_value='Dijkstra'))

You need to join twice, with different conditions for each. The conditions can be in the WHERE clause as RedFilter suggested, but I prefer compound conditionals in the JOIN clause for readability.

SELECT U.id 
FROM wp_users U
join wp_usermeta f on U.id = f.id 
                   and f.meta_key='first_name' 
                   and f.meta_value='MyFirstName'
join wp_usermeta l on U.id = l.id 
                   and l.meta_key='last_name' 
                   and l.meta_value='Dijkstra'

A slightly different version with the keys in the join and the requested values in the where clause:

SELECT U.id 
FROM wp_users U
join wp_usermeta f on U.id = f.id and f.meta_key='first_name' 
join wp_usermeta l on U.id = l.id and l.meta_key='last_name' 
where f.meta_value='MyFirstName' and l.meta_value='Dijkstra'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM