我正在使用Wordpress,我想得到
“帖子”表中的ID,其中posts.ID = postmeta.post_id和postmeta.distance> postmeta.radius
现在是SQL语句:

SELECT DISTINCT p.ID
FROM  posts p
LEFT JOIN  postmeta m ON p.ID = m.post_id
WHERE p.post_type = 'babysitter'
AND p.post_status = 'publish'
AND m.meta_key = 'distance'
AND ( m.meta_value > (SELECT meta_value
                       FROM postmeta
                       WHERE meta_key = 'radius' ))


除了避免嵌套SELECT的问题外,我没有得到我想要的东西

最佳答案

好吧,您的解释似乎很奇怪。您说postmeta.distance> postmeta.radius,但您从hlp_postmeta中选择了它,M别名似乎也无济于事。所以,我想您在这里有一些错别字,要么像您所说的那样从postmeta那里得到它,然后我想这就是您所需要的:

SELECT DISTINCT p.ID
FROM  posts p
LEFT JOIN  postmeta m ON p.ID = m.post_id
LEFT JOIN  postmeta m2 ON p.ID = m2.post_id
WHERE p.post_type = 'babysitter'
    AND p.post_status = 'publish'
    AND m2.meta_key = 'distance'
    AND m.meta_key = 'radius'
    AND m2.meta_value > m.meta_value


或者您不像您说的那样,从hlp_postmeta然后是:

SELECT DISTINCT p.ID
FROM  posts p
LEFT JOIN  postmeta m ON p.ID = m.post_id
LEFT JOIN  hlp_postmeta m2 ON p.ID = m2.post_id
WHERE p.post_type = 'babysitter'
    AND p.post_status = 'publish'
    AND m2.meta_key = 'distance'
    AND m.meta_key = 'radius'
    AND m2.meta_value > m.meta_value


当然,在两种情况下我都可能错了,如果这样告诉我,我会予以纠正。

08-06 21:45