1.DQL数据查询语言【查】
1.1简单查询
简单查询有两种方式:(1)查询所有数据(2)按不同字段名来查询数据.
简单查询数据,语法:
# 查询表内所有数据
select * from 表名;
# 查询表内数据,以指定的列来显示结果distinct为去除重复值关键词
select [distinct] 字段名1,字段名2,... from 表名;
1.2比较查询
使用此查询可以对数据进行条件筛选处理,通用语法:
select [*|字段名1, 字段名2, ...] from 表名 where 条件;
在where 条件
中,使用比较运算符来查询结果。
1.3范围查询
范围查询是指在某个范围内进行查询,分别有in和between...and...。
(1)in是用于非连续值的范围查询,语法:
select * from 表名 where 字段名 in (范围值1,范围值2,...);
(2)between and 是用于值在连续范围的查询,语法:
select * from 表名 where 字段名 between 范围值1 and 范围值2;
1.4逻辑查询
此查询是对数据进行条件筛选处理,通用语法:
select [*|字段名1, 字段名2, ...] from 表名 where 条件;
# 1
select * from product where price between 20 and 90;
# 逻辑运算+比较运算
select * from product where price >= 20 and price <= 100;
select * from product where 20 <= price and price <= 100; # 连贯/推荐
# 2
select * from product where price in (20,100);
select * from product where price = 20 or price = 100;
# 3
select * from product where price != 100; # 正向
select * from product where price = 100;
select * from product where not (price = 100);
# 4
select * from product where not (price = 20 or price = 100);
# 5
select * from product where price != 20 and price != 100;
select * from product where not (price in (20,100));
1.5模糊查询
模糊查询的语法:
select * from 表名 where 字段名 like '%某个字%';
或
select * from 表名 where 字段名 like '某个字_';
注:%表示任意多个任意字符,_表示一个任意字符。
下面举出一些例子,供哥们儿们更深入了解该语法。
#例如,使用命令完成:
#(1)查询商品名称含有"郑"字的所有商品信息;
#(2)查询商品名称为三个字的商品信息;
#(3)查询商品名称以"喜"结尾,并且是三个字的商品信息;
#(4)思考1:查询以"郑"开头,且是三个字的商品信息;
#(5)思考2:查询以"郑"开头的所有商品信息。
######################模糊查询##################################
# 1
select * from product where pname like '%郑%';
# 2
select * from product where pname like '_ _ _';
# 3
select * from product where pname like '_ _喜';
# 4
select * from product where pname like '郑_ _';
# 5
select * from product where pname like '郑%';
1.6非空查询
非空查询的语法:
select * from 表名 where 字段名 [条件];
-------------------------------
select * from biao where id is null;
select * from biao where id is not null;
非空运算符有:
1.7排序查询
查询语法:
select * from 表名 where 条件 order by 字段名 [asc|desc];
#asc 升序 (默认)
#desc 降序
1.8聚合查询
聚合查询的语法:
select 函数(count,max,min,sum,avg) from 表名 [where 条件];
后面会继续更新MySQL基础语法与操作。