我有两张表Students
和Books
学生:sid(数字),sname(varchar(3))
sid sname
---------
4 abc
2 def
1 ghi
3 jkl
图书:sid(数字),图书(varchar(1))
sid book
---------
3 a
2 b
2 c
1 u
4 d
1 k
3 p
4 q
4 k
现在,我想要输出如下所示(sid,sname和每个学生阅读的书籍数量)
sid sname count
---------------
1 ghi 2
2 def 2
3 jkl 2
4 abc 3
请帮助我。
答案 0 :(得分:1)
只需加入sid
列上的两个。
select s.sid, s.sname, count(b.book) cnt
from students s
left join books b
on s.sid = b.sid
group by s.sid, s.sname;
答案 1 :(得分:0)
select s.sid,s.sname,count(book) as count
from students as s
join books as b
on s.sid=b.sid
group by s.sid,s.sname
order by s.sid
答案 2 :(得分:0)
尝试此查询一次..
geocomplete
结果输出将类似于
SetJavaHome /usr/lib/jvm/java-7-openjdk-amd64/