• 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)

  • 相关阅读:
    Linux内核使用的GNUC扩展
    linux常用命令--开发调试篇
    代码示例_poll的多路复用
    硬件_红外传感器
    硬件_霍尔感应器
    全志_功能引脚配置_sys_config.fex
    知识_嵌入式常用词汇
    代码示例_Input 按键驱动
    Vmware_安装_tools
    Ubunt_配置_start
  • 原文地址:https://www.cnblogs.com/xuanhai/p/12971110.html
Copyright © 2020-2023  润新知