无法在内部联接上查询多个值

时间:2014-02-13 16:16:45

标签: mysql sql database wordpress

我正在使用此sql基于具有3个元值的内部联接返回结果。它似乎只与1 AND()一起使用,当我添加另外两个时,它返回0结果。

SELECT * FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND ( wp_postmeta.meta_key = 'plot_type' AND wp_postmeta.meta_value = 'Cottage' )
AND ( wp_postmeta.meta_key = 'number_of_bedrooms' AND wp_postmeta.meta_value = '2' )
AND ( wp_postmeta.meta_key = 'property' AND wp_postmeta.meta_value = '446' )
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;

4 个答案:

答案 0 :(得分:0)

我认为您打算与其他2使用OR(见下文)。同一个字段不能是2个不同的东西,这就是你得到0个结果的原因。

SELECT *
  FROM wp_posts
 INNER JOIN wp_postmeta
    ON (wp_posts.ID = wp_postmeta.post_id)
 WHERE wp_posts.post_type = 'plot'
   AND wp_posts.post_status = 'publish'
   AND ((wp_postmeta.meta_key = 'plot_type' AND
       wp_postmeta.meta_value = 'Cottage') OR
       (wp_postmeta.meta_key = 'number_of_bedrooms' AND
       wp_postmeta.meta_value = '2') OR (wp_postmeta.meta_key = 'property' AND
       wp_postmeta.meta_value = '446'))
 GROUP BY wp_posts.ID
 ORDER BY wp_posts.post_title ASC;

编辑,请尝试以下代码:

select * from wp_posts
join wp_postmeta on wp_posts.ID = wp_postmeta.post_id
where wp_posts.post_type = 'plot'
  and wp_posts.post_status = 'publish'
  and concat(wp_postmeta.meta_key,'|',wp_postmeta.meta_value)
        in ('plot_type|Cottage',
           'number_of_bedrooms|2',
           'property|446');

答案 1 :(得分:0)

您需要为每种类型的值加入wp_postmeta表一次。

SELECT whatever, whatever
  FROM wp_posts AS p

  JOIN wp_postmeta AS plottype 
    ON (p.ID = plottype.post_id AND plottype.meta_key = 'plot_type')

  JOIN wp_postmeta AS bedrooms
    ON (p.ID = bedrooms.post_id AND bedrooms.meta_key = 'number_of_bedrooms')

  JOIN wp_postmeta AS property
    ON (p.ID = property.post_id AND property.meta_key = 'property')

 WHERE wp_posts.post_type = 'plot'
   AND wp_posts.post_status = 'publish'

   AND plottype.meta_value = 'Cottage' 
   AND bedrooms.meta_value = '2'
   AND property.meta_value = '466'

 GROUP BY wp_posts.ID
 ORDER BY wp_posts.post_title ASC;

这个wp_postmeta键/值存储有点难以加入;您的加入条件需要提取相应的密钥以及匹配的帖子ID。

众所周知,SELECT *在软件中是一个坏主意。当你加入这么多牌桌时,这种情况尤其糟糕。列出结果集中所需的列。

另请注意,您使用INNER JOINJOIN同义。如果缺少从元数据中提取的任何值,那么结果集中的行也将丢失。你可能会或者可能不会更好地使用LEFT JOIN(你没有解释查询的目的。)

答案 2 :(得分:0)

似乎应该改变你的条件的某些结构。

尝试以下方法:

SELECT * FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE wp_posts.post_type = 'plot'
AND wp_posts.post_status = 'publish'
AND (
        (wp_postmeta.meta_key = 'plot_type' AND wp_postmeta.meta_value = 'Cottage')
        OR
        (wp_postmeta.meta_key = 'number_of_bedrooms' AND wp_postmeta.meta_value = '2')
        OR 
        (wp_postmeta.meta_key = 'property' AND wp_postmeta.meta_value = '446')
    )
ORDER BY wp_posts.post_title ASC;

答案 3 :(得分:0)

我设法使用WP_Meta_Query解决了这个问题,它生成的SQL是......

SELECT wp_posts.* FROM wp_posts 
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) 
INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) 

WHERE 1=1

AND wp_posts.post_type = 'plot'
AND (wp_posts.post_status = 'publish')
AND ( 
(wp_postmeta.meta_key = 'property' AND CAST(wp_postmeta.meta_value AS CHAR) = '180') 
  AND (mt1.meta_key = 'plot_type' AND CAST(mt1.meta_value AS CHAR) = 'Cottage') 
  AND (mt2.meta_key = 'number_of_bedrooms' AND CAST(mt2.meta_value AS CHAR) = '2')
) 
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC;

感谢大家的帮助:)