我在posts
表中有一些数据,在postmeta中我有一些meta_key
具有不同的值,例如:
meta_key
='手机'
meta_value
= 987654321
meta_key
='得分'
meta_value
= 40
很明显,对于电话号码,可以记录几个点 我希望得到一个电话号码的总分与其相关的帖子 那个查询看起来很复杂!
我尝试过的与此相似:
SELECT p.*, pm.* from wp_posts AS r
LEFT JOIN wp_postsmeta AS pm
ON pm.post_id = r.id
WHERE (pm.meta_key = 'score')
LEFT JOIN wp_postsmeta AS p
ON p.post_id = r.id
WHERE p.meta_key = 'phone'
我该如何实现?
答案 0 :(得分:1)
正确的语法是:
SELECT p.*, pms.meta_value as score, pmp.meta_value as phone
FROM wp_posts p LEFT JOIN
wp_postsmeta pms
ON pms.post_id = p.id AND pms.meta_key = 'score' LEFT JOIN
wp_postsmeta pmp
ON pmp.post_id = p.id AND pmp.meta_key = 'phone' ;
注意:
r
对wp_posts
没有意义,p
确实有意义。p.*
是一个非常奇怪的选择。我假设你真的想要帖子表。score
和phone
。where
子句在您的查询中都是错误放置和误用的。这些条件应该在on
条款中。编辑:
您修改后的问题似乎表明:
SELECT pmp.meta_value as phone, SUM(pms.meta_value) as score,
FROM wp_postsmeta pms LEFT JOIN
wp_postsmeta pmp
ON pmp.post_id = pms.post_id AND pmp.meta_key = 'phone' and pms.meta_key = 'score'
GROUP BY phone;