我正在尝试进行查询,但我无法掌握它。我想通过某些meta_keys选择所有记录。当我通过一个元键查询时,它工作正常。但是当我想通过两个meta_key进行查询时,它什么也没有返回。有人可以帮帮我吗?!
有效的查询:
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
并且不返回任何内容的查询:
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
我有3个表,表uszc_usermeta包含first_name和last_name,它们都在meta_value列中。
答案 0 :(得分:1)
您要求meta_key
等于两个' first_name'和' last_name'。您可能想要执行AND ub.meta_key IN ('first_name', 'last_name')
。
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 IN ('first_name', 'last_name')
AND acord = 'DA'
LIMIT 0 , 30
答案 1 :(得分:1)
您无法使用
创建任何记录AND ub.meta_key = 'first_name'
AND ub.meta_key = 'last_name'
这没有任何意义,所以就这么简单:
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'
OR ub.meta_key = 'last_name')
AND acord = 'DA'
LIMIT 0 , 30
更新由于OP说@bhelmet发布了正确的答案我想澄清OP 好查询应该如何看起来像imho:
SELECT u.*, ub.meta_value first_name, uc.meta_value last_name
FROM `uszc_users` AS u
INNER JOIN `uszc_utilizatori_acord` AS ua
ON ua.email = u.user_email
AND ua.acord = 'DA'
INNER JOIN `uszc_usermeta` AS ub
ON ub.user_id = u.id
AND ub.meta_key = 'first_name'
INNER JOIN `uszc_usermeta` AS uc
ON uc.user_id = u.id
AND uc.meta_key = 'last_name'
LIMIT 0 , 30
答案 2 :(得分:1)
要检查'usermeta'的两个条件,您应该连接两次表。你需要这样的东西:
SELECT *
FROM `uszc_users` AS u, `uszc_utilizatori_acord` AS ua, `uszc_usermeta` AS ub, `uszc_usermeta` AS uc
WHERE ua.email = u.user_email
AND ub.user_id = u.id
AND ub.meta_key = 'first_name'
AND uc.user_id = u.id
AND uc.meta_key = 'last_name'
AND acord = 'DA'
LIMIT 0 , 30