连接表以获取其余数据

时间:2012-08-13 16:04:26

标签: mysql

我已经运行了这段代码:

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' 
LEFT JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id AND wp_postmeta.meta_value = 'US' 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

只需选择wp_postmeta.meta_keywp_postmeta.meta_value,即_shipping_countryAU

需要检索这些字段中的更多数据。

所以,我假设我最后一起加入它,就像这样:

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' 
LEFT JOIN wp_posts ON wp_postmeta.post_id = wp_posts.id AND wp_postmeta.meta_value = 'US' 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')
LEFT JOIN wp_postmeta ON wp_posts.id = wp_postmeta.post_id
ORDER BY wp_postmeta.post_id DESC

然而这失败了?

1 个答案:

答案 0 :(得分:0)

更新了查询,因此它应该返回所有与shipping_country匹配“AU”,“US”,“JP”,“BR”或“UK”的订单的数据。

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' 
LEFT 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 EXISTS(SELECT * FROM wp_postmeta pm2 WHERE pm2.post_id = wp_posts.id AND pm2.meta_key = '_shipping_country' AND pm2.meta_value IN ('AU', 'US', 'JP', 'BR', 'UK'))
ORDER BY wp_postmeta.post_id DESC