一、表说明
同学生——成绩表2.1
二、目录
其他
1.查询"李"姓老师的数量
2.查询男生、女生人数
3.查询名字中含有"风"字的学生信息
4.查询同名同性学生名单,并统计同名人数
5.查询1990年出生的学生名单
6.查询各学生的年龄
7.查询本周过生日的学生
8.查询下周过生日的学生
9.查询本月过生日的学生
10.查询下月过生日的学生
三、查询
1.查询"李"姓老师的数量
select count(Tname) as '"李"姓老师的数量' from Teacher where Tname like N'李%'
2.查询男生、女生人数
方式1
select case when Ssex = '男' then '男生人数' else '女生人数' end '男女情况' , count(1) '人数' from student group by case when Ssex = '男' then '男生人数' else '女生人数' end
方式2
select sum(case when Ssex = '男' then 1 else 0 end) 男生人数, sum(case when Ssex = '女' then 1 else 0 end) 女生人数 from student
方式3 分开查
select count(Ssex) as 男生人数 from Student where Ssex = '男'
select count(Ssex) as 女生人数 from Student where Ssex = '女'
3.查询名字中含有"风"字的学生信息
4.查询同名同性学生名单,并统计同名人数
select Sname 学生姓名, count(*) 人数 from Student group by Sname having count(*) > 1
5.查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
select * from Student where year(sage) = 1990
6.查询各学生的年龄
方式1:只按照年份来算(x)
select * , datediff(yy , sage , getdate()) 年龄 from student
方式2:按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一(x)
select * , case when right( convert(varchar(10),getdate(),120),5) < right(convert(varchar(10),sage,120),5) then datediff(yy , sage , getdate()) - 1 else datediff(yy , sage , getdate() ) end '年龄' from student
方式3:
SELECT s.SNAME ,FLOOR((NOW()-s.Sage)/10000000000) 年龄 from student s
方式4:
SELECT SNAME,DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(Sage, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(Sage, '00-%m-%d')) AS 年龄 from student;
7.查询本周过生日的学生
方式1:(x)
select * from student where datediff(week,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = 0
方式2:
select * from student where DATE_FORMAT(Sage, '%c-%d') between DATE_FORMAT(date_sub(curdate(),INTERVAL WEEKDAY(curdate()) DAY), '%c-%d') and DATE_FORMAT(date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 6 DAY), '%c-%d');
8.查询下周过生日的学生(x)
方式1:(x)
select * from student where datediff(week,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = -1
方式2:
select * from student where DATE_FORMAT(Sage, '%c-%d') between DATE_FORMAT(date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 7 DAY), '%c-%d') and DATE_FORMAT(date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 13 DAY), '%c-%d');
9.查询本月过生日的学生(x)
方式1:(x)
select * from student where datediff(mm,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = 0
方式2:(注:date(concat(year(curdate()),'-',month(curdate()),'-','1')) 也是本月的第一天,和concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01')查询结果一致。)
select * from student where DATE_FORMAT(Sage, '%c-%d') between DATE_FORMAT(concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01'), '%c-%d') and DATE_FORMAT(LAST_DAY(now()), '%c-%d') order by DATE_FORMAT(Sage, '%c-%d')
10.查询下月过生日的学生(x)
方式1:(x)
select * from student where datediff(mm,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = -1
方式2:
select * from student where DATE_FORMAT(Sage, '%c-%d') between DATE_FORMAT(concat(date_format(LAST_DAY(now() + interval 1 month),'%Y-%m-'),'01'), '%c-%d') and DATE_FORMAT(LAST_DAY(now() + interval 1 month), '%c-%d') order by DATE_FORMAT(Sage, '%c-%d')