mysql上的内连接只能使用一列

时间:2011-12-05 18:01:02

标签: php mysql wordpress join inner-join

我不知道我是不是觉得愚蠢,但我无法解决这个问题:S

我正在试图从wp_posts表中获取项目,将其与表wp_postmeta连接两次(以获得不同的值)并使用这两个值创建条件。

SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.* FROM wp_posts
INNER JOIN wp_postmeta ds ON (ds.meta_key = 'date_start' AND ds.post_id = wp_posts.ID)
INNER JOIN wp_postmeta de ON (de.meta_key = 'date_end' AND ds.post_id = wp_posts.ID)
WHERE 1 = 1
  AND wp_posts.post_type = 'event'
  AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'closed' OR wp_posts.post_status = 'private' OR wp_posts.post_status = 'hidden')
  AND CAST(ds.meta_value AS DECIMAL(10,0)) <= 1323043200
  AND (CAST(de.meta_value AS DECIMAL(10,0)) >= 1323129599 OR de.meta_value = '')
ORDER BY ds.meta_value DESC
LIMIT 0, 5

我不知道为什么所有列只与'de' table

中的一行相关联

我的意思是:

ID meta_1 post_id meta_2
1     1       1    xxx
2     2       1    xxx
3     3       1    xxx
...

条件de.meta_key = 'date_end' ds.post_id = wp_posts.ID 我告诉wp_posts的ID应该等于来自'de' table(wp_postmeta)的post_id但是它没有得到尊重:|

我对错误的想法???

1 个答案:

答案 0 :(得分:2)

更改

(de.meta_key = 'date_end' AND ds.post_id = wp_posts.ID)
--> repeated match of ds.post_id=wp.posts.ID

(de.meta_key = 'date_end' AND de.post_id = wp_posts.ID)