博客
关于我
MySQL查询语句
阅读量:91 次
发布时间:2019-02-25

本文共 3370 字,大约阅读时间需要 11 分钟。

1.基本查询

  • 查询指定列的数据:

    select 字段1,字段2,字段3,... from 表名 where 条件语句;

  • 定义别名:

    select 字段1 别名1,字段2 别名2,字段3 别名3,... from 表名 where 条件语句;
    select 字段1 as 别名1,字段2 as 别名2,字段3 as 别名3,... from 表名 where 条件语句;

  • 消除重复:

    select distinct 字段名 from 表明 where 条件语句;

  • 限定结果集的行数:

    select top5 字段1,字段2,字段3,... from 表名 where 条件语句; //结果集的前5行
    select top10 percent 字段1,字段2,字段3,... from 表名 where 条件语句; //结果集的前10%行

  • 计算列的值:

    select sum1,sum2,sum1+sum2 as sum from 表名 [where clause];/ /计算并显示两数和

  • 聚合函数:

函数 功能
avg(<字段表达式>) 求一列数据的平均值
sum(<字段表达式>) 求一列数据的和
min(<字段表达式>) 求列中数据的最小值
max(<字段表达式>) 求列中数据的最大值
count(* I 字段名) 统计查询的行数

创建图表:在这里插入图片描述

2. 条件查询

  • 用于where子句的运算符
运算符 说明 运算符 说明
= 等于 <>或!= 不等于
< 小于 <= 小于等于
> 大于 >= 大于等于
is null 为空 is not null 不为空
and 并且 or 或者
in 包含 not in 不包含
between…and… 两者之间 not 用来取非
like 模糊查询(%:任意 _:一个 ) -------------------- --------------------------------
  • 算术运算查询:

    select 字段1,字段2,字段3 frome 表名 where 条件表达式;
    例:select * from t_student where point>800;

  • 带in关键字查询:

    select 字段1,字段2 frome 表名 where 字段 [not] in(元素1,元素2);
    例:select * from score where final in (60,80);

  • 带between and的范围查询:

    select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;
    例:select * frome score where final between 60 and 80;

  • 模糊查询:

    select 字段1,字段2… frome 表名 where 字段 [not] like ‘字符串’;
    “%”代表任意字符; “_”代表单个字符;
    例:select * frome student where sname like ‘梁%”;

  • 空值查询:

    select 字段1,字段2…frome 表名 where 字段 is[not] null;
    例:select * frome student where email is null;

  • 多条件查询(and):

    select 字段1,字段2…frome 表名 where 条件表达式1 and 条件表达式2 [and 条件表达式n]
    例:select * frome student where classno=‘080601’ and point>700;

  • 多条件查询(or)

    select 字段1,字段2…frome 表名 where 条件表达式1 or 条件表达式2 [or 条件表达式n]
    例:select * from student where classno=‘080501’ or classno=‘080601’;

  • 对查询结果排序order by:

    select 字段1,字段2…from 表名 order by 属性名 [asc|desc]
    例:select * frome student order by point desc;//降序,从大到小

  • 分组查询group by

    select 字段1, 字段2,...,聚合函数
    from 表明
    group by 字段名
    [having 条件语句];
    例:select studentno,courseno,avg(final) from score group by studentno having avg(final)>90;

  • 分页查询(limit):

    select 字段1,字段2,…from 表名 limit 初始位置,记录数;
    例:select * from student limit 0,4;

  • 合并查询(union | union all)

    select id from student
    union
    select id from score;

3.连接查询

  • select st.studentno,st.sname,st.classno,cl.department
    from student st,class cl
    where st.classno=cl.classno;

1.内连接查询(INNER)

  • select st.studentno,st.sname,st.classno,cl.department

    from student st inner join class cl
    on st.classno=cl.classno;

  • select st.studentno,st.sname,st.classno,sc.courseno,co.cname

    from student st join score sc
    on st.studentno=sc.studentno
    join course co
    on sc.courseno=co.courseno
    where st.sname='韩吟秋';

2.外连接查询OUTER(两张或以上的表连接起来查询某张表的信息)

  • 左连接查询

    select *
    from course co left outer join score sc
    on co.courseno=sc.courseno;

  • 右连接查询

    select *
    from course co right outer join score sc
    on co.courseno=sc.courseno;

4. 嵌套查询

  • 普通嵌套查询

    select * from score sc
    where sc.studentno=(select studentno from student where sname='韩吟秋');

  • 多值嵌套(ANY,ALL,IN,EXISTS)

    • 带in关键字的嵌套查询

      //查询与“何影”同班的学生学号、姓名和电话(使用子查询)
      select s.studentno,s.sname,s.phone
      from student s
      where s.classno in(
      select classno from student where sname ='何影') and s.sname!='何影';

    • 带any关键字的嵌套查询(any关键字表示满足其中任一条件)

      select * from student s where point> any(select point from student);

    • 带all关键字的嵌套查询(all关键字表示满足所有条件)

      select * from student s where point>= all(select point from student);

    • 带exists关键字的嵌套查询

      select * from student st
      where not exists(select * from score sc where sc.courseno='c05109' and st.studentno=sc.studentno);

转载地址:http://stn.baihongyu.com/

你可能感兴趣的文章
mysql 常见问题
查看>>
MYSQL 幻读(Phantom Problem)不可重复读
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
查看>>
MySQL 快速创建千万级测试数据
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>