mysql:查询语句

查询完整结构

select [distinct]* | 列名1,列名2,...[ as 别名]
from 数据源 [ as 别名]
[ where 条件表达式 ]
[ group by 分组字段 [ having 条件表达式 ] ]
[ order by 排序字段 [ asc | desc ] ]
[limit [行偏移] 行数目]

注:[] 表示可选输入项,| 表示或者。列名列表可使用 * 表示所有。

where

比较大小

可用命令:= 、< 、 <= 、 > 、 >= 、!= 、<> 、!> 、 !< 、<=>
注:<>等同!=,<=>用于比较null

指定范围

between ... and ... , not betweent ... and ...

匹配字符

可用命令:like , not like , rlike , not rlike , regexp , not regexp ,in , not in

注:
like用于模糊查询,rlike用于匹配正则表达式
in()匹配括号中的内容

select * from emp where a in(1,3,5);  //查询emp表中a等于1或者3或者5的信息

是否为空

可用命令:is null , is not noll

多条件查询

可用命令:and , or

group by

用于分组,一般搭配聚合函数使用

order by

按字段大小进行排序,默认为升序排序。asc,升序;desc 降序

select * from stu order by id desc; //按id降序排序查询stu表信息

distinct

去除重复的数据

select distinct sname , sex , birthday from stu; //查询stu表中的sname,sex,birthday的信息,有重复的只显示一次

limit

限制返回行数

select * from stu limit 2,3; //查询stu表,从第2行开始的3行信息,即2-4行
posted @ 2019-02-22 11:16  漓白白"  阅读(200)  评论(0编辑  收藏  举报