SELECT WHERE值> value2(从postmeta表中的另一行检索的value2)

时间:2016-03-03 13:59:34

标签: mysql wordpress

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

<div id="container">
  <div class="item" data-delay="200">
    <div class="item-in"></div>
  </div>
  <div class="item" data-delay="400">
    <div class="item-in"></div>
  </div>
  <div class="item" data-delay="600">
    <div class="item-in"></div>
  </div>
  <div class="item" data-delay="800">
    <div class="item-in"></div>
  </div>
  <div class="item" data-delay="200">
    <div class="item-in"></div>
  </div>
  <div class="item" data-delay="400">
    <div class="item-in"></div>
  </div>
  <div class="item" data-delay="200">
    <div class="item-in"></div>
  </div>
  <div class="item" data-delay="400">
    <div class="item-in"></div>
  </div>
</div>

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

1 个答案:

答案 0 :(得分:1)

嗯,你的解释似乎很奇怪..你说postmeta.distance&gt; 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

当然,在这两种情况下我都错了,如果是这样,请告诉我,我会纠正它。