这个SQL该如何写
写SQL语句:
表1:CLASS
含有两个字段:ID,CLASSNAME,表结构如下:
ID CLASSNAME
1 A
2 B
3 C
4 D
表2:STUDENT
含有如下字段:SID,NAME,CLASSID
SID是主键,CLASSID是外键,可以通过它和CLASS表作连接。
SID NAME CLASSID
01 你 1
02 我 3
03 他 4
........
要求写一SQL语句,查询出A,B,C,D班各有多少学生。
SQL code:
select id,className,'人数'=(select count(1) from student where classid=class.id) from class
SQL code:
SELECT SUM(sid), classid from student GROUP BY classid
不好意思,写错了,是count(sid)
select c.classname,count(s.sid) from class c,student s where c.id=s.classid group by c.classname
SQL code:
select a.classname,b.fc
from (
select classid fcid,count(*) fc
from student
group by classid
) a
left join class b on a.fcid=b.id
SQL code
C
相关问答:
数据库某表,想将其中f1,f2两个字段的内容翻10倍,请教如何写法?
update tbl set f1= f1*10,f2= f2*10 where id=10451
这种写法会造成plsql卡死
不会吧,那个id=10451 的数据有多少啊~~~~
我估计sql是不 ......
我的数据库的表是动态的表,在添加数据的时候有一个字段是DATETIME数据类型的,下面是存储过程
if exists (select * from sysobjects where name='proc_ADD_Order')
drop proc proc_ADD_Order
go
......
现在有表A(barcode varchar2,length number),B(barcode varchar2,serial number)其中barcode关联A:B为1:n
有如下sql语句
insert into a values('b1',10)
insert into a values( ......
表中是这样,
部门号 部门信息
1 部门一
2 部门二
3 部门三
4& ......