一. 检索一列或者多列
1.单独检索一列
select 列名 from 表名;
- 检索多列
select 列名1,列名2 … from 表名;
- 检索所有字段
select * from 表名;
二. 去除查询结果中的重复值
select distinct 列名1,列名2… from 表名;
三. 使用limit语句控制显示结果条目数
1.select 列名1,列名2… from 表名 limit 需要显示的行数;
2.select 列名1,列名2… from 表名 limit x,y;(x表示第几行开始显示,y表示显示的行数)
3.select 列名1,列2… from 表名 limit x offset y ; (x表示显示几行,y表示第几行开始显示)
四. 完全限制表名列名查询
select 完全列名 from 完全表名 ;
五. 对查询结果进行排序的order by 子句
1.针对单独列进行排序
select 列名 from 表名 order by 列名;
2.针对多列进行排序
select 列名 from 表名 order by 列名1,列名2;
3.降序排序
select 列名 from 表名 order by 列名 desc;
4.使用order by 子句和limit子句显示最大/最小值。
select 列名 from 表名 order by 列名 limit 1;
selec 列名 from 表名 order by 列名 desc limit 1;
六. where语句
(1) 使用 = 操作符限制查询
select 列名1, 列名2 from 表名 where 列1=‘值1’;
(2) 使用 < 操作符限制查询
select 列名1, 列名2 from 表名 where 列1<‘值1’;
(3)使用<>或者!=操作符限制查询
select 列1, 列2… from 表名 where 列1 <> ‘值1’;
(4) 使用between…and…操作符限制查询
select 列1,列2 … from 表名 where 列 between 值1 and 值2 ;(ps:包含两边的值)
(5)检索空值限制查询
select 列1,列2… from 表名 where 列 is null ;
(6) 检索非空值限制查询
select 列1,列2… from 表名 where 列 is not null ;
(7) 使用and 操作 (且)
select 列1 ,列2… from 表名 where 限制条件1 and 限制条件2 ;
(8)使用or操作(或)
select 列1,列2 … from 表名 where 限制条件1 or 限制条件2 ;
(9)and 优先级 高于 or ;
select 列1,列2…from 表名 where 列1限制条件 or 列2限制条件 and 列3 限制条件;
(10)使用 in 操作符 (或)
select 列1 ,列2 … from 表名 where 列 in (值1,值2,值3);
(11) 使用 not in 操作符
select 列1 ,列2 … from 表名 where 列 not in (值1,值2 ,值3);
七.like 操作
select 列1 ,列2 … from 表名 where 列 like ’关键字与通配符‘;
八.regexp操作 - 进行匹配(.)
select 列1 ,列2… from 表名 where 列1 regexp ‘.000’;
- 强制区分大小写,可以使用关键字binary.
select 列1,列2 … from 表名 where 列1 regexp binary ’关键字‘;
- 执行or 匹配
select prod_id,prod_name from products where prod_name regexp ’1000|2000|3000|’;
- 使用字符集匹配
select prod_id,prod_name from products where prod_name regexp ‘[1235] ton’;
- 匹配特殊字符(添加转义符)
- 使用元字符匹配查询