对多行进行连接查询以进行搜索
目前我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法同时将一行连接到另一个表的另外两行,因此您需要连接另一个表两次,一次用于名字,一次用于姓氏。像这样:
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:
您需要加入两次,每次都有不同的条件。正如 RedFilter 建议的那样,条件可以位于 WHERE 子句中,但为了可读性,我更喜欢 JOIN 子句中的复合条件。
稍微不同的版本,其中连接中的键和 where 子句中请求的值:
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.
A slightly different version with the keys in the join and the requested values in the where clause:
通过重写 AND 的顺序来考虑逻辑(不,括号没有区别)。
看到问题了吗?这个条件永远不会成立,因为
US.meta_key
和US.meta_value
都不会是两个值。您需要使用OR
,而不是AND
。编辑
这样的查询将为您提供所需的结果:
Consider the logic by re-writing the order of the
AND
s (no, parens don't make a difference).See the problem? This condition will never be true, because neither
US.meta_key
norUS.meta_value
will never be two values. You need to use anOR
, not andAND
.EDIT
A query like this would give you the desired results: