zoukankan      html  css  js  c++  java
  • 常用典型的sql语句

    1、两张表,怎么把一张表中的数据插入到另一张表中?

    1,insert into table_a select * from table_b

    2,insert into table_a(field_a1,field_a2,field_a3) select field_b1,field_b2,field_b3 from table_b

    2、查询总score>100 cno

    查询:select cno,sum(score) from sc group by cno having sum(score)>100

    判断:select cno,sum(score) from sc where sum(score)>100 and cno=#{cno}

    3、查出数据前几条最大的

    MYSQLselect t.* from (select sno,cno from sc order by cno desc limit 0,3) t order by sno

    4、有一张table-t,里面有三个字段:英语,其中有三条记录分别表示语文70,数学80,英语58,请用一条sql语句查询出这三条记录并按以下条件显示出来:
    大于或等于80表优秀,大于等于60表及格,小于60表不及格显示格式:
    及格 优秀 不及格

    select t.yuwen as '语文',t.shuxue as '数学',t.english as '英语' from

    (select case when yuwen>=80 then '优秀' when yuwen>=60 then '及格' else '不及格' end yuwen,

    case when shuxue>=80 then '优秀' when shuxue>=60 then '及格' else '不及格' end shuxue,

    case when english>=80 then '优秀' when english>=60 then '及格' else '不及格' end english

    from table_t) t

    4、计算及格率

    select concat(sum(case when score>60 then 1 else 0 end)/count(*)*100,'%') as score from sc

    5、 将某列的行内容变成列字段名

    变为

    select name,sum(case when sub='语文' then sco end) '语文',
    SUM(case when sub='数学' then sco end)'数学',SUM(case when sub='英语' then sco end) '英语'
    from score1 GROUP BY name

    6、查出每门课都大于60的学生的姓名

    有以下两种方法:

    1select name from score group by name having min(sco)>=60

    2select distinct name from score1 where name not in(select DISTINCT name from score1 where sco<=60);

    7、查询平均分大于80的学生姓名

    select name from score1 GROUP BY name having avg(sco)>80

    注:随时更新

  • 相关阅读:
    J2那几个E和Web基础
    PHP开发人员对JAVA的WEB开发入门(初版-基础知识)
    一个处理大数据的后台服务(已废弃)
    一个请求过来都经过了什么
    请一定记得升级java虚拟机
    面向对象之 结构体和类的区别
    Swift 结构体的使用
    iOS 波浪效果的实现
    iOS 常用三方(持续更新)
    Xshell 链接 Could not connect to '192.168.80.129' (port 22): Connection failed
  • 原文地址:https://www.cnblogs.com/jincheng81/p/9038435.html
Copyright © 2011-2022 走看看