简体   繁体   中英

Condition with left join table in mysql

I have Quiz table which is shown in image. 测验表

and quiz result history table likeuser_quiz_results 表

I write left join query between these two table. My query is like:

  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

This query give result all quiz table data and related user_quiz_results table.

If I write query using condition for left join table like userid=90, I don't get the any of data.

I want all data of quiz and if table have result for userid=90 get data from table 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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