当前位置:   article > 正文

.NET/C#汇总 —— 数据库SQL查询(附建表语句)

.NET/C#汇总 —— 数据库SQL查询(附建表语句)

1.⽤⼀条SQL 语句 查询出每⻔课都⼤于80 分的学⽣姓名

建表语句:

  1. create table tableA
  2. (
  3. name varchar(10),
  4. kecheng varchar(10),
  5. fenshu int(11)
  6. )DEFAULT CHARSET = 'utf8';

插⼊数据

  1. insert into tableA values ('张三','语⽂',81);
  2. insert into tableA values ('张三','数学',75);
  3. insert into tableA values ('李四','语⽂',76);
  4. insert into tableA values ('李四','数学',90);
  5. insert into tableA values ('王五','语⽂',81);
  6. insert into tableA values ('王五','数学',100);
  7. insert into tableA values ('王五','英语',90);

答案有3种:

  1. 答案A:
  2. select distinct name from tableA where name not in (select distinct name fr
  3. om tableA where fenshu<=80)
  4. 答案B:
  5. select name from tableA group by name having min(fenshu)>80
  6. 答案C:
  7. select name from tableA group by name having count(kecheng)>=3 and min(fens
  8. hu)>=80

2.⼀道SQL语句⾯试题,关于group by表内容:

2005-05-09 胜

2005-05-09 胜

2005-05-09 负

2005-05-09 负

2005-05-10 胜

2005-05-10 负

2005-05-10 负

如果要⽣成下列结果, 该如何写sql语句?

 建表语句:

  1. create table tableb (
  2. rq varchar(10),
  3. shengfu nchar(1)
  4. )DEFAULT CHARSET = 'utf8';

 插⼊数据:

  1. insert into tableb values('2005-05-09','胜');
  2. insert into tableb values('2005-05-09','胜');
  3. insert into tableb values('2005-05-09','负');
  4. insert into tableb values('2005-05-09','负');
  5. insert into tableb values('2005-05-10','胜');
  6. insert into tableb values('2005-05-10','负');
  7. insert into tableb values('2005-05-10','负')

答案:

  1. select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when
  2. shengfu='负' then 1 else 0 end)'负' from tableb group by rq

3.⼀个叫 team 的表,⾥⾯只有⼀个字段name, ⼀共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四 个球对进⾏⽐赛,⽤⼀条sql 语句显示所有可能的⽐赛组合

team表建表语句:

  1. CREATE TABLE team (
  2. `name` varchar(20)
  3. ) DEFAULT CHARSET=utf8;

插⼊数据:

  1. INSERT INTO team VALUES ('a');
  2. INSERT INTO team VALUES ('b');
  3. INSERT INTO team VALUES
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/511870
推荐阅读
相关标签
  

闽ICP备14008679号