簡體   English   中英

Sql查詢加入四個表

[英]Sql query to join four tables

SELECt 
    qst_id,qst_title,ans_date,ans_text 
FROM
    (
        SELECT 
            a.Question_Id as qst_id,a.Question_Title as qst_title,a.Question_Text as qst_text,DATE_FORMAT(a.LastActivity_Date,'%d %b %Y %T') as qst_date,b.UserForum_Image as qst_prof,b.ScreenName as qst_scname

        FROM 
            tblforumquestion a, tblregistration2_2 b 
        WHERE  a.RegistrationId=b.RegistrationId and a.LastActivity_Date between '2014-0-01 00:00:00' and '2015-05-01 00:00:00'
        ORDER BY a.LastActivity_Date desc limit 5

        outer join

        SELECT 
            DATE_FORMAT(c.Answer_Date,'%d %b %Y %T')  as ans_date,c.Answer_Text as ans_text,d.UserForum_Image as ans_prof,d.ScreenName as ans_scname
        FROM 
            tblforumanswer c ,tblregistration2_2 d
        where c.Answer_Id in 
            ( 
                SELECT  MAX(Answer_Id)
                FROM tblforumanswer
                GROUP BY Question_Id 
            )  
        and c.RegistrationId=d.RegistrationId 
        order by c.Answer_Date desc limit 5
    )

我試圖從我的帖子中得到最新的5個問題和答案。如果沒有答案的任何問題都存在,它也應該在一行中顯示為具有空答案細節的問題詳細信息。但是上面的代碼正在收到錯誤。任何幫助都是明顯的。我的數據庫是mysql。

tblquest

tblans

結果

tblquest tblans結果

我想我們終於提取了足夠的細節來得出答案:

select q.qstid, q.qsttext, a.anstext
  from tblquest q
    left join tblans a
      on q.qstid = a.qstid
    left join tblans a2
      on a.qstid = a2.qstid and a.ansdate < a2.ansdate
  where a2.ansdate is null
  order by q.qdate desc limit 5;

在這里演示

我們left join了問題的答案,以確保我們保留所有問題,包括那些沒有答案的問題。

然后我們再次left join答案,但這次是在一個范圍條件下,以便選擇最近的問題答案。 如果沒有a2的日期大於a ,則a必須是最近的答案 - 這將由where a2.ansdate is null子句的where a2.ansdate is null過濾。

如果您願意,也可以使用子查詢來完成。

最后,我們只是訂購並限制我們的結果,以便獲得最近的5個問題。

外連接語法有問題。 檢查評論和樣本數據。

SELECT
    qst_id,qst_title,ans_date,ans_text 
FROM
    (
        SELECT 
            a.Question_Id as qst_id,a.Question_Title as qst_title,a.Question_Text as qst_text,DATE_FORMAT(a.LastActivity_Date,'%d %b %Y %T') as qst_date,b.UserForum_Image as qst_prof,b.ScreenName as qst_scname

        FROM 
            tblforumquestion a, tblregistration2_2 b 
        WHERE  a.RegistrationId=b.RegistrationId and a.LastActivity_Date between '2014-0-01 00:00:00' and '2015-05-01 00:00:00'
        ORDER BY a.LastActivity_Date desc limit 5

        outer join  --Error comes here

        SELECT 
            DATE_FORMAT(c.Answer_Date,'%d %b %Y %T')  as ans_date,c.Answer_Text as ans_text,d.UserForum_Image as ans_prof,d.ScreenName as ans_scname
        FROM 
            tblforumanswer c ,tblregistration2_2 d
        where c.Answer_Id in 
            ( 
                SELECT  MAX(Answer_Id)
                FROM tblforumanswer
                GROUP BY Question_Id 
            )  
        and c.RegistrationId=d.RegistrationId 
        order by c.Answer_Date desc limit 5
    )

--This is example of outer join
SELECT 
    A.*, B.*
FROM 
    TableA a outer join TableB b on a.RegistrationId = b.RegistrationId

請參閱鏈接了解更多詳情:

完全外部加入MySQL

https://dev.mysql.com/doc/refman/5.0/en/outer-join-simplification.html

http://www.w3schools.com/sql/sql_join_full.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM