时间:2021-07-01 10:21:17 帮助过:3人阅读
以上是表的创建
1 SELECT A.* FROM tonghua AS A INNER JOIN tonghua AS B ON A.ID = B.ID-1 2 WHERE A.主叫号码=B.主叫号码 AND DATEDIFF(MINUTE,A.通话结束时间,B.通话起始时间)> 10 UNION SELECT B.* FROM tonghua AS B INNER JOIN tonghua AS A ON A.ID = B.ID-1 3 WHERE A.主叫号码=B.主叫号码 AND DATEDIFF(MINUTE,A.通话结束时间,B.通话起始时间)> 10展开代码
1 select a.*,b.* from 2 tonghua a,tonghua b where b.id> a.id and b.id=(select min(id) from tonghua where id> a.id 3 and (a.主叫号码=b.主叫号码 or a.主叫号码=b.被叫号码 or a.被叫号码=b.主叫号码 or a.被叫号码=b.被叫号码)) 4 and DATEDIFF(minute,a.通话结束时间,b.通话起始时间)>=10 5 and DATEDIFF(second,a.通话结束时间,b.通话起始时间)>=10展开代码
以上是我认为正确的方法。
2、用一条select语句求出所有课程在80分(含80分)以上的学生姓名,请写出所有可行方案。
1 use QQmdb 2 go 3 if OBJECT_ID(‘sc‘) is not null 4 drop table sc 5 create table sc 6 ( 7 name char(10), 8 kc char(10), 9 score decimal(5,2) 10 ) 11 12 insert into sc 13 values(‘张三‘,‘语文‘,80), 14 (‘张三‘,‘数学‘,81), 15 (‘张三‘,‘英语‘,100), 16 (‘李四‘,‘语文‘,90), 17 (‘李四‘,‘数学‘,80), 18 (‘李四‘,‘英语‘,70), 19 (‘王五‘,‘语文‘,100), 20 (‘王五‘,‘数学‘,100), 21 (‘王五‘,‘英语‘,79)展开代码
以上是表的创建
1 select name from sc group by name having min(score)>=80
1 select name from sc where score >=80 group by name having COUNT(score)>=3 --先根分数大于80分的姓名分组,因为是3科,所以要分数count>3展开代码
1 select distinct a.name from sc a,sc b, sc c where a.name=b.name and b.name=c.name and a.name=c.name and a.kc<>b.kc and a.kc<>c.kc and b.kc<>c.kc and a.score>=80 and b.score>=80 and c.score>=80展开代码
1 select distinct name from sc where name not in (select name from sc where score<80) --不推荐用这种展开代码
3、分别要求出每个team粉丝数最多的3个国家
fans(team,nationality,fanCount)
‘Barcelona‘,‘Germany‘,12000
‘Barcelona‘,‘Spain‘,18000
‘Barcelona‘,‘China‘,13000
‘Barcelona‘,‘USA‘,15000
‘Barcelona‘,‘Italy‘,11000
‘Real Madrid‘,‘France‘,19000
‘Real Madrid‘,‘Germany‘,13000
‘Real Madrid‘,‘USA‘,17000
‘Real Madrid‘,‘Korea‘,16000
‘Real Madrid‘,‘UK‘,12000
1 select * from fans 表 where 2 ( select count(*) from fans t2 where t1.team=t2.team 3 and t2.fanCount>=t1.fanCount ) <=3 --子查询较牺牲性能展开代码
还有一些借书问题,根据书号,时间,做的一些查询。。。
常见的sql面试题
标签: