sql查询的问题(嵌套AND OR)

时间:2011-03-21 22:13:52

标签: mysql database wordpress

我正在尝试使用以下查询,但它没有获取任何记录:

SELECT wp_posts.* FROM wp_posts 
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
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 ( wp_term_relationships.term_taxonomy_id IN (92) ) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private' OR wp_posts.post_status = 'future') 
AND wp_postmeta.meta_key = '_inicio_date' 
AND mt1.meta_key = '_inicio_date' 
AND mt2.meta_key = '_eventtimestamp' 
AND ( (mt1.meta_value BETWEEN 20110201 AND 20110231) OR (mt2.meta_value BETWEEN 20110201 AND 20110231) ) 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date asc  

但是,如果我这样做:

SELECT wp_posts.* FROM wp_posts 
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)   
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 ( wp_term_relationships.term_taxonomy_id IN (92) ) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private' OR wp_posts.post_status = 'future') 
AND wp_postmeta.meta_key = '_inicio_date' 
AND mt1.meta_key = '_inicio_date' 
AND mt2.meta_key = '_eventtimestamp' 
AND (mt1.meta_value BETWEEN 20110201 AND 20110231) 
OR (mt2.meta_value BETWEEN 20110201 AND 20110231) 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date asc

它获取记录,但忽略了使用“OR”

找到的记录的所有previos“AND”

如何嵌套最后的“AND”和“OR”?

由于

2 个答案:

答案 0 :(得分:0)

在我看来,wp_posts中的一个条目可能没有wp_postmeta表中的两个连接记录,所以更改

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) 

LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) 
LEFT JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) 

还有一件事:当你使用BETWEEN时要小心:

为了将BETWEEN与日期或时间值一起使用时获得最佳结果,请使用CAST()将值显式转换为所需的数据类型。示例:如果将DATETIME与两个DATE值进行比较,请将DATE值转换为DATETIME值。如果在与DATE的比较中使用字符串常量(如“2001-1-1”),则将字符串强制转换为DATE。

答案 1 :(得分:0)

它使用了这个:

SELECT wp_posts.* FROM wp_posts 
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 
LEFT JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) 
LEFT JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) 
WHERE ( wp_term_relationships.term_taxonomy_id IN (".$categoria.") ) 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private' OR     wp_posts.post_status = 'future') 
AND ( ( mt1.meta_key = '_incio_date' AND (mt1.meta_value BETWEEN 20110201 AND 20110231) ) OR (mt2.meta_key = '_eventtimestamp' AND (mt2.meta_value BETWEEN 20110201 AND 20110231) ) )
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date asc