我希望内连接中的限制1来自三个表中的一个表和来自第一个表的所有记录

时间:2016-01-23 07:01:13

标签: mysql subquery

当我执行以下查询时;

SELECT wp_posts.ID,wp_posts.post_title, wp_posts.post_date,wp_users.display_name 
FROM wp_posts 
INNER JOIN wp_users ON wp_posts.post_author=wp_users.ID 
INNER JOIN wp_comments ON wp_comments.comment_post_ID=wp_posts.ID 
INNER JOIN (
   SELECT comment_author,comment_date,comment_content 
   FROM wp_comments  
   ORDER BY comment_date DESC LIMIT 1) b ON b.comment_post_ID=wp_posts.ID 
WHERE wp_posts.post_type='app-forum' 
ORDER BY wp_posts.post_date DESC

我收到此错误

#1054 - Unknown column 'b.comment_post_ID' in 'on clause' 

2 个答案:

答案 0 :(得分:0)

您的子查询未提供该列:

SELECT comment_author,comment_date,comment_content, **-- no comment_post_ID here**
FROM wp_comments  
ORDER BY comment_date DESC LIMIT 1) b

b视为“ad-hoc表”(子查询生成的所有列都必须具有明确的名称)。

答案 1 :(得分:-1)

必须在内部联接查询中选择这样的comment_post_ID

SELECT comment_author,
       comment_date,
       comment_content,
       comment_post_ID <-- here you have to select
FROM wp_comments
ORDER BY comment_date DESC LIMIT 1