繁体   English   中英

mysql中左连接表的条件

[英]Condition with left join table in mysql

我有测验表,如图所示。 测验表

和测验结果历史记录表,如user_quiz_results 表

我在这两个表之间编写了左连接查询。 我的查询是这样的:

  select q.*
       , c.name as catname 
       , qrh.q_complete
       , q s.test_type
       , qs.questionid
       , sum(qrh.score) as score1
       , sum(qrh.total_questions) as tot_que 
    from categories c
       , quiz q 
    left 
    join user_quiz_results_history qrh 
      on qrh.quiz_id = q.quizid 
    left 
    join quizquestions qq 
      on qq.quizid = q.quizid 
    left
    join questions qs 
      on qs.subcatid = qq.subjectid 
   where c.catid = q.catid 
     AND q.catid = 1
     AND q.status = 'Active' 
     AND q.enddate >= '2016-05-02' 
   group 
        by q.quizid

此查询给出结果所有测验表数据和相关的 user_quiz_results 表。

如果我使用像 userid=90 这样的左连接表的条件编写查询,我不会得到任何数据。

我想要测验的所有数据,如果表中有 userid=90 的结果,则从表 user_quiz_history.avg(score) 中获取数据。

在左连接子句上写条件。

select q.* , qrh.q_complete , sum(qrh.score) as score1 , sum(qrh.total_questions) as tot_que from categories c , quiz q left join user_quiz_results_history qrh on qrh.quiz_id = q.quizid AND qrh.userid=84 where  q.catid = 1 AND q.status = 'Active' AND q.enddate >= '2016-05-02' group by q.quizid

暂无
暂无

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

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