对多行进行连接查询以进行搜索

发布于 2024-11-08 14:11:41 字数 881 浏览 0 评论 0原文

目前我正在开发一个 Wordpress 网站,我希望能够在我网站的一部分上根据用户的名字和姓氏搜索用户。

Wordpress 使用元表来存储用户的任何“额外”数据。名字和姓氏是其中一些元字段。

现在我正在尝试创建一个查询,它得到一个结果,该结果返回一个与名字和姓氏匹配的用户,唯一的问题是有一个元行用于姓氏,另一个元行用于名字,它们都有对同一 user_id 的引用。只是我不知道如何正确创建查询。

当我有这个查询时,

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')

它确实返回我正在寻找的用户,但是当我尝试这个时

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')

,我根本没有得到任何结果,有人可以帮助我吗?

如果有人对数据库结构感兴趣,这里( http://cl.ly/6rGx )是用户表,这里( http://cl.ly/6rts )是 users_meta 表。

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月苍老的讽刺 2024-11-15 14:11:41

您无法同时将一行连接到另一个表的另外两行,因此您需要连接另一个表两次,一次用于名字,一次用于姓氏。像这样:

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'

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'
独守阴晴ぅ圆缺 2024-11-15 14:11:41

您需要加入两次,每次都有不同的条件。正如 RedFilter 建议的那样,条件可以位于 WHERE 子句中,但为了可读性,我更喜欢 JOIN 子句中的复合条件。

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'

稍微不同的版本,其中连接中的键和 where 子句中请求的值:

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'

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'
胡渣熟男 2024-11-15 14:11:41

通过重写 AND 的顺序来考虑逻辑(不,括号没有区别)。

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

看到问题了吗?这个条件永远不会成立,因为 US.meta_keyUS.meta_value 都不会是两个值。您需要使用OR,而不是AND

编辑

这样的查询将为您提供所需的结果:

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'))

Consider the logic by re-writing the order of the ANDs (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'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文