2016-01-23 20 views
0

當我執行以下查詢時;我想從三個表中的一個表內的第一個表中的一個表內部連接限制1

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' 
+0

你有'SELECT COMMENT_AUTHOR選擇'comment_post_ID', COMMENT_DATE, COMMENT_CONTENT FROM wp_comments ORDER BY comment_date DESC LIMIT 1' –

+0

考慮提供適當的創建和插入語句以及期望的結果 – Strawberry

回答

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爲「臨時表」(通過所產生的所有列子查詢必須有明確的名稱)。

+0

謝謝,但現在我的查詢被執行,但我沒有從評論標籤中獲取記錄勒 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(SELECT COMMENT_AUTHOR,comment_post_ID,COM​​MENT_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 = '應用論壇' ORDER BY wp_posts.post_date DESC –

-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 
+0

SELECT wp_posts.ID,wp_posts.post_title,wp_posts.post_date,wp_users.display_name FROM wp_posts INNER加入wp_users ON wp_posts.post_author = wp_users.ID INNER JOIN( SELECT comment_author,comment_date,comment_content,comment_post_ID FROM wp_comments GROUP BY comment_post_ID ORDER BY comment_date DESC )b ON b.comment_post_ID = wp_posts.ID WHERE wp_posts。 post_type ='app-forum'ORDER BY wp_posts.post_date DESC 但是現在我還沒有從評論表中獲得列,但是我從用戶和帖子表中只獲取了列 –

+0

@ShridharReddy發生了什麼事? –

+0

爲什麼要投票? @downvoter –

相關問題