且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用左表的条件执行右外连接

更新时间:2022-10-23 21:54:18

I发现它,谢谢:)

  SELECT  p.rollno,p.name, case   s.statuss = '  A' 然后 COUNT(p.rollno) else  '  0'  end   as 计算
来自出勤s
正确 outer join n>学生p
s.rollno = p.rollno
s 。 date ' 10-09-2013' ' 13-10-2013'
group by p.rollno,p.regno,p.name,s.statuss
order by p .rollno;


I have two tables,

student:
rollno | name
1      | Abc
2      | efg
3      | hij
4      | klm

attendance:
name | date       |statuss
Abc  | 10-10-2013 | A
efg  | 10-10-2013 | A
Abc  | 11-10-2013 | A
hij  | 25-10-2013 | A



my required output is:
some query with where condition as "where date between '10-09-2013' and '13-10-2013' "

rollno| name |count
1     | Abc  | 2
2     | efg  | 1
3     | hij  | 0
4     | klm  | 0


i tried using:

SELECT p.rollno,p.name,case when s.statuss='A' then COUNT(p.rollno) else '0' end as count from attendance s right outer join student p on s.rollno=p.rollno where s.date between '10-09-2013' and '13-10-2013' group by p.rollno,p.regno,p.name,s.statuss order by p.rollno

output is:
rollno| name |count
1     | Abc  | 2
2     | efg  | 1



i want the remaining values from the student table to also be appended,i tried many ways all are in vain,pls provide me a query which provides the output,
thanks in advance.

I found it,thank you :)
SELECT p.rollno,p.name,case when s.statuss='A' then COUNT(p.rollno) else '0' end as count
from    attendance s
        right outer join student p
            on s.rollno=p.rollno
            and s.date between '10-09-2013' and '13-10-2013'
group by p.rollno,p.regno,p.name,s.statuss
order by p.rollno;