我遇到了MySQL查询问题。我应该按年龄选择成员使用年龄的上限和下限。例如,此查询返回一些结果
SELECT DISTINCT users.*
FROM wp_users as users
INNER JOIN wp_usermeta as mt
ON (users.ID = mt.user_id)
WHERE 1=1
AND (
(mt.meta_key LIKE 'user_birthday' AND mt.meta_value <> '' AND mt.meta_value <= 1985)
AND (mt.meta_key LIKE 'user_birthday' AND mt.meta_value <> '' AND mt.meta_value >= 1920)
)
AND users.ID NOT IN (1,2,3)
ORDER BY users.user_registered desc
此查询返回约70个结果。但是当我在这个查询中添加搜索名字时,就像这样:
SELECT DISTINCT users.*
FROM wp_users as users
INNER JOIN wp_usermeta as mt ON (users.ID = mt.user_id)
WHERE 1=1
AND (
(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%first_name::John%')
AND (mt.meta_key LIKE 'user_birthday' AND mt.meta_value <> '' AND mt.meta_value <= 1985)
AND (mt.meta_key LIKE 'user_birthday' AND mt.meta_value <> '' AND mt.meta_value >= 1920)
)
AND users.ID NOT IN (1,2,3)
ORDER BY users.user_registered desc
这会返回0结果,但我知道我在数据库中有2个Johns。
SELECT DISTINCT users.*
FROM wp_users as users
INNER JOIN wp_usermeta as mt ON (users.ID = mt.user_id)
WHERE 1=1 AND (
(mt.meta_key = '_upme_search_cache' AND mt.meta_value LIKE '%first_name::John%')
) AND users.ID NOT IN (1,2,3)
ORDER BY users.user_registered desc
这个返回2条记录。
这里肯定没有语法错误,但我想我的逻辑在某处?
我应该提一下,这是安装了UPME插件的WordPress数据库。
我是否在加入桌子或别的东西时犯了错误?
答案 0 :(得分:1)
这可能会有所帮助..
$args = array(
'meta_query' => array( 'meta_query' => array(
array(
'key' => '_upme_search_cache',
'value' => 'first_name::John',
'compare' => 'LIKE'
),
array(
'key' => 'user_birthday',
'value' => '1985',
'compare' => '<='
),
array(
'key' => 'user_birthday',
'value' => '1920',
'compare' => '>='
)
)),
'include' => array(),
'exclude' => array(1,2,3),
'orderby' => user_registered ,
'order' =>DESC
);
$users = get_users( $args );
答案 1 :(得分:1)
问题是您要查询不在同一行中的数据(meta_key列的值不同)。您需要另一个JOIN到wp_usermeta
表或一些子查询。您可以使用此小提琴作为点http://sqlfiddle.com/#!9/48836/2