MySQL单表查询

简单查询
通过条件查询
查询排序限制查询记录数
使用集合函数查询
分组查询
使用正则表达式查询

一、简单查询

简单查询
select * from 表名;
select 字段1,字段2 from 表名;
避免重复DISTINCT

select DISTINCT 字段1 from 表名;
注:不能部分使用DISTINCT,通常仅用于某一字段。

通过四则运算查询
select 数值字段*任意数字 from 表名;
显示美观可按照以下写法
select 数值字段 * 任意数字 as 字段别名 from 表名;

定义显示格式
concat()函数用于连接字符串
示例:
select concat(name,‘annual_salary’,salary*14) AS Annual salary from employee;

二,条件查询
单条件查询
select 字段1,字段2 from 表名 where 字段1 = ‘ ’;

多条件查询

select 字段1,字段2 from 表名 where 字段1 =‘ ’ and 字段2 > 100;

关键字BETWEEN AND
select * from 表名 where 字段1 between 500 and 1000;

select * from 表名 where 字段1 not between 500 and 1000;

关键字IS NULL
select * from 表名 where 字段1 is null;

select * from 表名 where 字段1 is not null;

关键字IN集合查询

select * from 表名 where 字段1 = 400 or 字段1 = 500 or 字段1 = 600;
等效于下面这句
select * from 表名 where 字段1 IN(400,500,600);

关键字LIKE模糊查询

通配符‘%’

select * from 表名 where 字段1 LIKE ‘al%’;
查询以al开头的字段

通配符‘_’
** select * from 表名 where 字段1 LIKE ’ al __’; **一个下划线代表一个任意字符

三,查询排列
按单列排序

select * from 表名 ORDER BY 字段1;
select * from 表名 ORDER BY 字段1 ASC; //升序
select * from 表名 ORDER BY 字段1 DESC;//降序

按多列排序
select * from 表名 ORDER BY 字段1 DESC,字段2 ASC;

四,限制查询记录数
示例:
select * from 表名 order by 字段1 desc LIMIT 5;//限制查询5行

select * from 表名 order by 字段1 desc LIMIT 3,5;//从第四条开始,共显示5条

五,使用集合函数查询

select count(*) from 表名;//查询该表有几条数据

还有**MAX( ) , MIN( ) , AVG( ) , SUM( ) **函数

六,分组查询
示例:

select dep_id,GROUP_CONCAT(NAME) FROM EMPLOYEE5 GROUP BY dep_id;

MySQL单表查询
MySQL单表查询

七,使用正则表达式查询

select * from 表名 where 字段1 REGEXP ‘^ali’;
select * from 表名 where 字段1 REGEXP ‘yun$’;
select * from 表名 where 字段1 REGEXP ‘m{2}’;