鉴于这两行中的值,为什么我的查询返回0行以及如何重写查询才能正常工作?
SELECT * FROM wp_postmeta
WHERE post_id = 73
AND ( meta_key = 'wpcf-mw' AND meta_value BETWEEN -8 AND 200 )
AND ( meta_key = 'wpcf-milk' AND meta_value BETWEEN 20 AND 200 )
SQL查询:
{{1}}
答案 0 :(得分:2)
id
通过OR更改AND。
答案 1 :(得分:0)
基本上有两种方法可以做到这一点。一个大致如下:
SELECT post_id
, MAX(CASE WHEN meta_key = 'wpcf-milk' THEN meta_value END) wpcf_milk
, MAX(CASE WHEN meta_key = 'wpcf-mw' THEN meta_value END) wpcf_mw
FROM wp_postmeta
GROUP
BY post_id
HAVING wpcf_milk BETWEEN 20 AND 200
AND wpcf_mw BETWEEN -8 AND 200;
另一种方法涉及OUTER JOIN并且速度稍快,但输入时稍微繁琐一点 - 所以我会留给其他人。