数据库基础MySQL(三 有关数据查询)

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基础语法与操作。

  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值