关于wp_user和user_meta问题的MySQL查询

时间:2016-12-22 作者:Nelson101

我有一个查询,当我有一个field view\\u type=sale时可以使用,但当view\\u type=lead时,即使我知道有记录,它也不会返回任何记录。这是查询。。。

    SELECT a.id, a.user_email, a.user_registered, a.user_login, b1.meta_value AS first_name, b2.meta_value as last_name, b3.meta_value as qualified,
b4.meta_value as referrer,
b5.meta_value as view_type,
b6.meta_value as ref_by,
b7.meta_value as wp_optimizemember_custom_fields
FROM wp_users a
INNER JOIN wp_usermeta b1 ON b1.user_id = a.ID AND b1.meta_key = \'first_name\' 
INNER JOIN wp_usermeta b2 ON b2.user_id = a.ID AND b2.meta_key = \'last_name\'
INNER JOIN wp_usermeta b3 ON b3.user_id = a.ID AND b3.meta_key = \'qualified\'
INNER JOIN wp_usermeta b4 ON b4.user_id = a.ID AND b4.meta_key = \'referrer\'
INNER JOIN wp_usermeta b5 ON b5.user_id = a.ID AND b5.meta_key = \'view_type\'
INNER JOIN wp_usermeta b6 ON b6.user_id = a.ID AND b6.meta_key = \'ref_by\'
INNER JOIN wp_usermeta b7 ON b7.user_id = a.ID AND b7.meta_key = \'wp_optimizemember_custom_fields\'

WHERE (b4.meta_value LIKE \'$user_ID\' AND b5.meta_value LIKE \'lead\') or
(b6.meta_value LIKE \'$user_ID\' AND b5.meta_value LIKE \'lead\') 
这个查询有什么问题?

2 个回复
SO网友:CodeMascot

在里面usermeta WordPress表存储许多序列化数据。如此直接meta_value 我想这行不通。因此,请尝试以下查询-

SELECT a.id, 
       a.user_email, 
       a.user_registered, 
       a.user_login, 
       b1.meta_value AS first_name, 
       b2.meta_value AS last_name, 
       b3.meta_value AS qualified, 
       b4.meta_value AS referrer, 
       b5.meta_value AS view_type, 
       b6.meta_value AS ref_by, 
       b7.meta_value AS wp_optimizemember_custom_fields 
FROM   wp_users AS a 
       INNER JOIN wp_usermeta AS b1 
               ON b1.user_id = a.id 
                  AND b1.meta_key = \'first_name\' 
       INNER JOIN wp_usermeta AS b2 
               ON b2.user_id = a.id 
                  AND b2.meta_key = \'last_name\' 
       INNER JOIN wp_usermeta AS b3 
               ON b3.user_id = a.id 
                  AND b3.meta_key = \'qualified\' 
       INNER JOIN wp_usermeta AS b4 
               ON b4.user_id = a.id 
                  AND b4.meta_key = \'referrer\' 
       INNER JOIN wp_usermeta AS b5 
               ON b5.user_id = a.id 
                  AND b5.meta_key = \'view_type\' 
       INNER JOIN wp_usermeta AS b6 
               ON b6.user_id = a.id 
                  AND b6.meta_key = \'ref_by\' 
       INNER JOIN wp_usermeta AS b7 
               ON b7.user_id = a.id 
                  AND b7.meta_key = \'wp_optimizemember_custom_fields\' 
WHERE  ( b4.meta_value LIKE \'%{$user_ID}%\' 
         AND b5.meta_value LIKE \'%lead%\' ) 
        OR ( b6.meta_value LIKE \'%{$user_ID}%\' 
             AND b5.meta_value LIKE \'%lead%\' ) 
看这里我把所有的meta_value\'两个之间的s值%. 这意味着它将接受序列化数据并将其作为字符串进行搜索。

希望这有帮助。

SO网友:Nelson101

好的,问题是1条记录没有“ref\\u by”meta\\u键集,并且具有内部连接的所有记录都必须成功,这就是为什么我没有得到结果。