• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
半壁江山
博客园    首页    新随笔    联系   管理    订阅  订阅

SQL实例操作

部分例子参见:https://zhuanlan.zhihu.com/p/38354000

1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名

name   kecheng   fenshu
张三    语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90

select name from table group by name having min(fenshu)>80

2. 学生表 如下:
自动编号   学号   姓名 课程编号 课程名称 分数
1        2005001 张三 0001     数学    69
2        2005002 李四 0001      数学    89
3        2005001 张三 0001      数学    69
删除除了自动编号不同, 其他都相同的学生冗余信息

delete tablename where 自动编号 not in(select min( 自动编号) from tablename group by学号, 姓名, 课程编号, 课程名称, 分数)

3.怎么把这样一个表
year   month amount
1991   1     1.1
1991   2     1.2
1991   3     1.3
1991   4     1.4
1992   1     2.1
1992   2     2.2
1992   3     2.3
1992   4     2.4
查成这样一个结果
year m1   m2   m3   m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4 

select year,case when month=1 then amount as m1,case when month=2 when amount as m2,case when month=3 then amount as m3, case when month=4 then m4 end

from A

group by year

4. 说明:复制表( 源表名:a新表名:b) 

select * into b from a where 1<>1(只复制表结构)

select * into b from a where 1=1 (复制表结构和数据内容)

 insert into b(a, b, c) select d,e,f from a; 

5.写出一条SQL语句:取出表A中第31到第40条记录,ID作为自增主键,ID可能不是连续的。

select  top 10 * from A where ID not in(select top 30 ID from A order by ID)

 

posted @ 2020-05-27 10:43  半壁江山  阅读(598)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3