0

我有以下 SQL:

SELECT * FROM wp_postmeta 
INNER JOIN wp_term_relationships ON wp_postmeta.post_id = wp_term_relationships.object_id AND wp_term_relationships.term_taxonomy_id = '10' 
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id WHERE wp_posts.post_date BETWEEN CONCAT(CURDATE(),' 10:00:00') AND CONCAT(CURDATE(),' 18:00:00')
WHERE wp_postmeta.meta_key = '_shipping_country' AND wp_postmeta.meta_value IN ('AU', 'US', 'JP', 'BR', 'UK')
ORDER BY wp_postmeta.post_id DESC

为什么它不起作用的任何原因,但如果我删除第二个INNER JOIN它工作正常?

4

4 回答 4

2

你不能有WHERE内部连接条件。

SELECT * FROM wp_postmeta 

INNER JOIN wp_term_relationships ON wp_postmeta.post_id = wp_term_relationships.object_id AND wp_term_relationships.term_taxonomy_id = '10' 
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id AND wp_posts.post_date BETWEEN CONCAT(CURDATE(),' 10:00:00') AND CONCAT(CURDATE(),' 18:00:00')
WHERE wp_postmeta.meta_key = '_shipping_country' AND wp_postmeta.meta_value IN ('AU', 'US', 'JP', 'BR', 'UK')
ORDER BY wp_postmeta.post_id DESC
于 2012-08-13T13:22:11.800 回答
0

是的:虽然可能有一些wp_postmeta具有相应wp_term_relationships的 ,而一些具有wp_posts,但没有两者兼有。

要了解有关您的数据的更多信息,请将您的联接更改为LEFT JOIN

于 2012-08-13T13:22:34.217 回答
0

同一个查询中不能有两个WHERE子句。

您需要做的就是将第一个WHERE条件移到现有子句中,或者将其AND设置为 Join:

任何一个:

SELECT * FROM wp_postmeta 
INNER JOIN wp_term_relationships ON wp_postmeta.post_id = wp_term_relationships.object_id AND wp_term_relationships.term_taxonomy_id = '10'  
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id AND wp_posts.post_date BETWEEN CONCAT(CURDATE(),' 10:00:00') AND CONCAT(CURDATE(),' 18:00:00') 
WHERE wp_postmeta.meta_key = '_shipping_country' AND wp_postmeta.meta_value IN ('AU', 'US', 'JP', 'BR', 'UK') 
ORDER BY wp_postmeta.post_id DESC 

或者:

SELECT * FROM wp_postmeta       
INNER JOIN wp_term_relationships ON wp_postmeta.post_id = wp_term_relationships.object_id AND wp_term_relationships.term_taxonomy_id = '10'  
INNER JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id 
WHERE wp_postmeta.meta_key = '_shipping_country' AND wp_postmeta.meta_value IN ('AU', 'US', 'JP', 'BR', 'UK') 
AND wp_posts.post_date BETWEEN CONCAT(CURDATE(),' 10:00:00') AND CONCAT(CURDATE(),' 18:00:00') 
ORDER BY wp_postmeta.post_id DESC 
于 2012-08-13T13:23:20.497 回答
0

您有两个“WHERE”语句。那可能是个问题:)

于 2012-08-13T13:22:14.897 回答