ER
显示的时候注意看要不要用group by … having …语法
查找男生女生的人数:SELECT ssex,COUNT(ssex) FROM student GROUP BY ssex
注意有left join 或者 right join 的时候要count的话,可能有null,所以count()里面一般不是*,是count(某个表的某个字段名)
查询每门课程的选课人数,即使课程没有人选,也要显示出来,按人数降序排列。显示中文列名:课程号、课程名、选课人数。
select C.CNO as课程号,CNAME as 课程名,count(SC.SNO) as 选课人数 from C lef join SC on C.CNO=SC.CNO group by C.CNO,CNAME order by count(SC.SNO) desc
group by xxxxxx having xxxxx
查询同名同性学生名单,并统计同名人数,输出格式为 sname,ssex,数
//group by后面就是一个整体了
select sname,ssex,count() as 数量 from Student group by sname,ssex having count()>1
第一按系别升序,第二按学号降序
select * from student order by sdept asc,sno desc
连接查询不用 inner join
select * from tablea,tableb,tablec where tablea.x=tableb.x and tableb.y=tablec.y and …
要升序或者降序必须要使用oder by 语句
select 城市,count() as 人数 from 购物者 group by 城市 having count()>=2
from tablea inner join tableb on tablea.c=tableb.c group by tablea.c,d order by e desc —这里是order by后只属于一个表的字段前不用加表名,若共有,需要加一个表名,如tablea.c
阴:列名是英文,显示要中文。如:select SNO as 学号, Sname as 姓名, AGE as 年龄, SEX as 性别 from S where SNO in (select Sno from SC where CNO=‘1001’)
阴:money类型数据和整型一样,insert into table1 values(’’,’’,100)
ps:insert into table1(s#,sname,ssex) values(‘001’,‘刘德华’,‘男’)
当要求默认值为0的时候,不管是什么数据类型,都直接写整型的0,例如新增一条选课记录,学号:08,课程号:01,成绩默认设置为0 insert into sc(s#,c#,score) values(‘08’,‘01’,0)
字段为空用is null:select * from 购物者 where 联系电话 is null
year(getdate())-year(出生日期)
select distinct 城市 from 购物者
#select sum(订单细节.数量*商品.价格) as 总金额 from 商品 inner join 订单细节 on 商品.商品编号=订单细节.商品编号 where 订单细节.订单编号=‘2020010001’
datetime类型的日期也是要加单引号,例如:‘1999-12-12’
题目里有“请编写SQL语句来实现”或“请编写查询语句来实现”的题,都另加一行写go
and SNO in (select Sno from SC where CNO=‘1003’)
1.select S.SNO as 学号,SNAME as 姓名, AVG(grade) as 平均分 from S inner join SC on S.SNO=SC.SNO group by S.SNO, SNAME order by AVG(grade) DESC
group by 学号,姓名,order by 平均分
2.select 购物者编号,姓名,联系电话 from 购物者 where 联系电话 like ‘135%’
注意电话号码带’'了
3.select 购物者编号,姓名,城市 from 购物者 where 城市 in (‘成都’,‘乐山’)
注意in (‘城市’,‘乐山’)
|