Mysql在条件相关的查询中

时间:2014-01-28 06:26:51

标签: php mysql

SELECT ac1.id, ac1.course_name, case when a.yettoapprove is not null 
 then 'true'
 else 'false'
 end OrderedAll , a.* from (SELECT ac.id, sum(case when ad.status = '2' then 1 else 0 end) as yettoapprove , sum(case when ad.status = '1' then 1 else 0 end) as approved,case when ad.status = '2' is not null 
 then 'true'
 else 'false'
 end OrderedAll FROM aw_ou_student_data ad , jos_users ju , aw_ou_lookup_courses ac,aw_ou_lookup_colleges ac1 where ac1.id=ju.college_code and ac.id in (27,28,29,30,133,32,33,34,35,36,37,38,39,40,41,42,43,44,134,135) and ac.school_id=2 and ju.id=ad.user_id and ac.id=ju.course_code GROUP BY ac.id ORDER BY ac.id ) a left join  aw_ou_lookup_courses ac1 on ac1.id = a.id

在上面的上述内部查询中,我已经给出了20 ac.id,但结果只得到了14条记录。如何获得总共20条记录。即如果不满足则出现在条件中的id我应该在该id旁边有一个0作为值的记录。

我该怎么做..?

1 个答案:

答案 0 :(得分:1)

带条件的简单左连接应该这样做:

SELECT
    table1.id,
    table2.*
FROM
    table table1
    LEFT JOIN table2 ON table1.id = table2.id AND (your conditions here)