简体   繁体   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
    )

I am trying to get latest 5 question and answers from my post.if any question without answer is there,it should also display as question details in one row with null answer details.But the above code is getting error.Any help is appreciable.my database is mysql. 我试图从我的帖子中得到最新的5个问题和答案。如果没有答案的任何问题都存在,它也应该在一行中显示为具有空答案细节的问题详细信息。但是上面的代码正在收到错误。任何帮助都是明显的。我的数据库是mysql。

tblquest

tblans

结果

tblquest tblans result tblquest tblans结果

I think we've finally extracted enough detail to arrive at an answer: 我想我们终于提取了足够的细节来得出答案:

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;

demo here 在这里演示

We left join the answers to the questions, in order to ensure we have keep all questions, including those without answers. 我们left join了问题的答案,以确保我们保留所有问题,包括那些没有答案的问题。

We then left join to the answers again, but this time on a range condition in order to just pick off the most recent answer to the question. 然后我们再次left join答案,但这次是在一个范围条件下,以便选择最近的问题答案。 If there is no a2 with a date greater than a , then that a must be the most recent answer - this is filtered for by the where a2.ansdate is null clause. 如果没有a2的日期大于a ,则a必须是最近的答案 - 这将由where a2.ansdate is null子句的where a2.ansdate is null过滤。

That could also be accomplished with a subquery if you preferred. 如果您愿意,也可以使用子查询来完成。

Finally, we just order and limit our results in order to get the 5 most recent questions. 最后,我们只是订购并限制我们的结果,以便获得最近的5个问题。

Problem with your outer join syntax. 外连接语法有问题。 Check the comment and sample data. 检查评论和样本数据。

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

Refer link for more detail: 请参阅链接了解更多详情:

Full Outer Join in MySQL 完全外部加入MySQL

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM