zoukankan      html  css  js  c++  java
  • sql查询语句详解

    SQL查询语句详解(一)

    一、基本语法

    Select  select_list
    From  table_name
    Where  condition_expression
    Group by  group_columns   
    having condition_expression
    Order by sort_columns
    

    二、查询实例

    • 查询所有字段
    • 查询指定字段
    • 用DISTINCT去除结果中的重复行
    • 查询指定数据
    • 带有in关键字的查询
    • 带between and的范围查询
    • 带like的字符匹配查询

    表1:学生

    • 结构

    在这里插入图片描述

    • 数据

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Bo45955-1598920526193)(assets/1594220880779.png)]

    表2:商品表1

    • 结构:

    在这里插入图片描述

    • 数据:

    在这里插入图片描述

    表3:商品表2

    • 结构:

    在这里插入图片描述

    • 数据

    在这里插入图片描述

    表4:教师

    • 结构:

    在这里插入图片描述

    • 数据
      在这里插入图片描述

    在这里插入图片描述

    • 数据分析需求

    1)查询所有学生的详细信息

    SELECT *from 学生;
    

    2)查询学生的学生号,姓名与专业

    select 学生号,姓名,专业 from 学生;
    

    3)从商品表1中查询出每一种商品的价值

    SELECT 商品代号,单价,数量,单价*数量 as 总价 from 商品表1;
    

    4)查询本商品表1中商品的种类

    SELECT distinct 分类名 from 商品表1;
    

    5)从商品表1中查询出分类名为"电视机"的所有商品

    select * from 商品表1 where 分类名 ='电视机';
    

    6)从商品表1中查询出单价低于2000元的每一种商品代号,分类名和单价

    select 商品代号,分类名,单价 from `商品表1`  where 单价<=2000;
    

    7)从商品表2中查询出产地为北京、山西、无锡的所有商品

    SELECT * from 商品表2 where 产地 in('北京','山西','无锡');
    

    8)从商品表1中查询出商品代号以字符串"dsj"开头的所有商品(考察like模糊查询及通配符%)

    select * from 商品表1  where 商品代号 like 'DSJ%'
    

    MySQL中的通配符;%, _ (下划线),

    9)从商品表1中查询单价在1000到2000之间的所有商品

    select * from 商品表1 where 单价 between 1000 and 2000;
    

    10)查询所有姓张的学生的详细信息

    select * from 学生 where 姓名 like '张_';
    

    11)从商品表1中查询出单价大于1500,同时数量大于等于10的商品

    select * from 商品表1 where 单价 >=1500 and 数量 >=10;
    

    12)从教师表中查询联系电话为空的教师信息

    select * from 教师 where 联系电话 is null;
    

    13)从选课表中查询按课程号的升序,同一课程按成绩降序排列

    select * from 选课  order by 课程号 asc,成绩 desc;
    

    14)从学生表中查询各个专业的人数

    select 专业,count(*) as 人数 from 学生 group by 专业
    

    15)从学生表中查询出专业的学生数多于1人的专业名及人数

    select 专业,count(*) as 人数 from 学生 group by 专业 having count(*)>1
    select 专业,count(*) as 人数 from 学生 group by 专业 having 人数>1
    

    16)从选课表中查询成绩最高前三位

    select * from 选课 order by 成绩 desc limit 3
    

    17)查询各个专业男女人数各是多少

    select 专业,性别,count(*) as 人数 from 学生 group by 专业,性别
    

    18)从商品表1中查询出所有商品的最大数量、最小数量、平均数量及数量总和

    select max(数量) as 最大数量,min(数量) as 最小数量,avg(数量) as 平均数量,sum(数量) as 数量总和 from 商品表1 
    

    19)从商品表1中查询出分类名为“电视机”的商品的种数、最高价、最低价及平均价

    select 分类名,count(分类名) as 种数,max(单价) as 最高价,min(单价) as 最低价,avg(单价) as 平均价 from 商品表1 where 分类名='电视机'
    

    20)查询出产地为南京或无锡的所有商品的商品代号、分类名、产地和品牌

    select a.商品代号,b.分类名,a.产地,a.品牌 from 商品表2 a inner join 商品表1 b on a.商品代号=b.商品代号 where a.产地 in ('南京','无锡')
    

    select a.商品代号,b.分类名,a.产地,a.品牌 from 商品表2 a,商品表1 b
    where a.商品代号=b.商品代号 and a.产地 in ('南京','无锡')
    

    21)从教学库中查询出选修了课程名为“操作系统”课程的每个学生的姓名

    select b.姓名,c.课程名 from 选课 a 
    inner join 学生 b on a.学生号=b.学生号 
    inner join 课程 c on a.课程号=c.课程号 where c.课程名='操作系统' or b.姓名
    

    select 姓名,课程名 from 选课 a,学生 b,课程 c where a.学生号=b.学生号 and a.课程号=c.课程号 and c.课程名='操作系统'
    

    22)从教学库中查询出所有学生的选课情况,要求没选修任何课程的学生信息也要
    反映出来(左,右连接)

    select *from 学生 a left join 选课 b on a.学生号=b.学生号
    select * from 选课 a right join 学生 b on a.学生号=b.学生号
    

    23)从教学库中查询出选修了课程名为“操作系统”的所有学生(带in的子查询)

    select * from 学生 where 选修了课程名为“操作系统”的所有学生
    进一步分析:
    select * from 学生 where 学生号 in (所有选修了操作系统的学生号名单)
    所有选修了操作系统的学生号名单?
    select a.学生号 from 选课 a,课程 b  where a.课程号=b.课程号 and 课程名='操作系统'
    最终的代码:
    select * from 学生 where 学生号 in (select a.学生号 from 选课 a,课程 b  where a.课程号=b.课程号 and 课程名='操作系统')
    
    

    24)查询出比所有商品单价的平均值要高的全部商品(比较运算符)

    select * from 商品表1 where 单价>(select avg(单价) from 商品表1)
    

    25)从教学库中查询出选修至少一门课程的所有学生(带exists(的子查询)

    select * from 学生 where exists(select * from 选课 where 选课.学生号=学生.学生号)
    

    select * from 学生
    where  学生号 in ( select distinct 学生号  from 选课 )
    

    26)从教学库中查询出选修了课程名为“C++语言“的所有学生的姓名和成绩(any子查询)

    select b.姓名,c.课程名,a.成绩 from 选课 a
    inner join 学生 b on a.学生号=b.学生号
    inner join 课程 c on a.课程号=c.课程号 where c.课程名='C++语言'
    
    select 姓名,成绩 from 学生 join 选课 on 学生.学生号=选课.学生号
    where 课程号=any(select 课程号 from 课程 where 课程名='C++语言')
    

    27)从商品表1中查询出单价比分类名为 “洗衣机”的所有商品单价都高的商品

    select *from 商品表1 where 单价>all (select 单价 from 商品表1 where 分类名='洗衣机')
    select * from 商品表1 where 单价>(select max(单价)from 商品表1 where 分类名='洗衣机')
    

    28)从商品表1中查询出单价比分类名为“洗衣机”的所有商品的单价 其中一种 高的商品

    select * from 商品表1 where 单价>any(select 单价 from 商品表1 where 分类名='洗衣机') and 分类名<>'洗衣机'
    

    29)查询出每种商品的总价值,并按降序排列

    select 分类名,sum(单价*数量) as 总价 from 商品表1 group by 分类名 order by 总价 desc
    

    30)查询至少选修了王明所选修的所有课程的学生

    -- 王明选修的课程
    select a.姓名,b.课程号 from 学生 a,选课 b where a.学生号=b.学生号 and 姓名='王明'
    
    -- 终极答案
    select*from 学生
    where 姓名 !='王明' and 学生号 in 
    (select 学生号 from 选课 where 课程号 in (select 课程号 from 选课,学生 where 选课.学生号=学生.学生号 and 姓名='王明') 
    group by 学生号 having count(*)=(select count(*) from 选课,学生 where 选课.学生号=学生.学生号 and 姓名='王明'))
    
    -- 终极答案2
    select*from 学生
    where 姓名 !='王明' and 学生号=any 
    (select 学生号 from 选课 where 课程号 in (select 课程号 from 选课,学生 where 选课.学生号=学生.学生号 and 姓名='王明')
     group by 学生号 having count(*)=(select count(*) from 选课,学生 where 选课.学生号=学生.学生号 and 姓名='王明'))
    

    31)查找选择了课程编号为101和102的学生,把其学号显示出来

    合并查询(union去除重复记录)

    select * from 选课成绩 where 课程编号 in(101,102)
    
    select * from 选课成绩 where 课程编号='101'
    union 
    select *from 选课成绩 where 课程编号='102' 
    union
    select *from 选课成绩 where 课程编号='103' 
    

    如果希望上述结果中显示课程名称,sql语句如下:

    select a.*,b.课程名称 from 选课成绩 a,课程2 b 
    where a.课程编号=b.课程编号 and a.课程编号='101'
    union
    select a.*,b.课程名称 from 选课成绩 a ,课程2 b
    where a.课程编号=b.课程编号 and a.课程编号='102'
    

    32)把test1和test2表的数据合并在一起

    select * from test1 
    union
    select * from test2
    

    在这里插入图片描述

    合并查询(union all 保留重复记录)

    select * from test1 
    union all
    select * from test2
    

    在这里插入图片描述

    三、其他补充

    33)创建如下表并插入测试数据

    在这里插入图片描述

    要求对此表查询,显示如下结果:

    显示结果:
    姓名 语文 高数 英语
    李勇 90 70 80
    刘晨 60 77 96

    select  a.name as 姓名,a.score as 语文,b.scoreas 高数,c.score as 英语 
    from
    grade a,grade b,grade c 
    where
    a.name =b.name and b.name=c.name 
    and 
    a.subject='语文' and b.subject='高数' and c.subject='英语'
    

    在这里插入图片描述

    34)在SQL中使用正则示例

    查询以DS开头的产品
    select * from 商品表1 where 商品代号 regexp '^DS'
    查询以冰箱结尾的
    select * from 商品表1 where 分类名 regexp '冰箱$'
    查询商品代号中包含B字符的产品
    select * from 商品表1 where 商品代号 regexp 'B.'
    查询商品代号中包含XT
    select * from 商品表1 where 商品代号 REGEXP '[XT]' 
    查询商品代号中不包含XT的
    select * from 商品表1 where 商品代号 REGEXP '[^XT]'
    查询test表中id不包含a-h之间任意一个字符的学员
    select * from test where id REGEXP '[^a-h]'
    查询出商品表1中商品代号包含DB,XY,AH
    select * from 商品表1 where 商品代号 REGEXP 'DB|XY|AH'
    查询商品表中至少有1个X的商品
    select * from 商品表1 where 商品代号 REGEXP 'X{1}'
    查询商品表1中商品代号中,XY最少出现1,XY最多出现3select * from 商品表1 where 商品代号 REGEXP 'XY{1,3}'
    

    35)高级插入语句

    insert into test1(id,name,age) select id, name,age from test2 where id =106
    

    36)replace语句
    用replace插入一条记录时,如果不重复,replace就和insert
    的功能一样,如果有重复记录,replace就使用新记录的值来替换原来的记录值。
    语法:

    replace tablename(列名...)VALUES(列值);replace tablename SET column_namel =value1,column_name2 = value2,..;
    

    示例:

    replace test1(id,name ,age) values (104,'陈阳',22)
    

    37) 将电子专业的所有学生各门课成绩加5分(techdb 成绩表)

    update 选课 set 成绩=成绩+5
    where 学生号 in (select 学生号 from 学生 where 专业='电子')
    

    13、将员工表中工龄大于30年的工资全部上调20%(northwind)

    select 姓名,部门,year(now())-year(雇佣日期) as 工龄 from 
    员工 where year(now())-year(雇佣日期)>30
    
    update 员工 set 目前薪资=目前薪资*1.2 where year(now())-year(雇佣日期)>30
    

    14、将比生产制造部员工最高工资还要高的员工工资下调20%
    以下这种写法是错误的:

    以下代码是错误的:
    
    select 姓名,部门,目前薪资 from 员工
    where 目前薪资>(select max(目前薪资) from 员工 where 部门='生产制造部')
    
    正确的写法是:
    
    update 员工 set 目前薪资=目前薪资*0.8
    where 目前薪资>(select t.最高工资 from (select max(目前薪资) as 最高工资 from 员工 where 部门='生产制造部') t)
    

    14、将比生产制部中工资排名前三的员工工资任意一个高的员工工资下调10%

    select 姓名,部门,目前薪资 from 员工 where 目前薪资>
    any(select 目前薪资 from 员工 where 部门 ='生产制造部' order by 
    目前薪资 desc limit 3) and 部门<>'生产制造部'
    
    正确的代码是:
    
    update 员工 set 目前薪资=目前薪资*0.9 where 目前薪资>
    any(select t.目前薪资 from  (select 目前薪资 from 员工 where 部门 ='生产制造部' order by 
    目前薪资 desc limit 3) t ) and 部门<>'生产制造部'
    

    如果想把以上条件的员工删除,则SQL代码是:

    delete from  员工 where 目前薪资>
    any(select t.目前薪资 from  (select 目前薪资 from 员工 where 部门 ='生产制造部' order by 目前薪资 desc limit 3) t ) and 部门<>'生产制造部'
    
  • 相关阅读:
    使用paramikoHelper类实现MySQL安装和数据恢复
    tornado-模板,转义,上传静态文件
    tornado-请求与响应
    tornado-输入
    tornado-输出,request
    配置Ubuntu虚拟环境
    tornado-简单的服务器非阻塞
    Linux查看进程,端口,访问url
    tornado-简单的服务器
    字符串,数组,定时器,form
  • 原文地址:https://www.cnblogs.com/James-221/p/13647447.html
Copyright © 2011-2022 走看看