MySQL查询语法

前期准备的数据

# 创建数据库
create database if not exists shopping charset utf8;
# 选择数据库
use shopping;
# 创建产品表
create table product
(
    id      int primary key,
    name    varchar(20),
    price   int,
    type varchar(20),
    address varchar(20)
);
# 创建类型表
create table type(
    id int primary key,
    name varchar(20)
);
# 插入产品数据
insert into shopping.product(id, name, price, type, address) values
    (1,'商品1',200,'type1','北京'),
    (2,'商品2',400,'type2','上海'),
    (3,'商品3',600,'type3','深圳'),
    (4,'商品4',800,'type1','南京'),
    (5,'商品5',1000,'type2','成都'),
    (6,'商品6',1200,'type3','武汉'),
    (7,'商品7',1400,'type1','黑龙江'),
    (8,'商品8',1600,'type2','黑河'),
    (9,'商品9',1800,'type3','贵州'),
    (10,'商品10',2000,'type1','南宁');
# 插入类型数据
insert into shopping.type(id,name) values
    (1,'type1'),
    (2,'type2'),
    (3,'type4');

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cZu4aNQe-1659435588353)(SQL基础知识数据查询.assets/image-20220228111020846.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7PmkD6fu-1659435588353)(SQL基础知识数据查询.assets/image-20220228111041294.png)]

一、简单查询

注意事项

  • 字段名可以指定一个,也可以指定多个,使用*代表查询所有字段
  • 表名必须存在
  • select后边可以对指定资源进行数学计算

select 字段名 from 表名;

# 使用*可以查询到当前表中的所有字段
select * from product;
# 查询所有商品的指定字段(商品名,商品价格)
select name,price from product;
# 查询所有商品价格的同时对所有的商品价格+10后进行输出
select name,price + 10 from product;

二、比较查询

< > <= >= = != <>

# 查询商品价格大于600的所有商品信息
select * from product where price > 600;
# 查询商品价格小于2000的所有商品信息
select * from product where price < 2000;
# 查询商品价格小于等于1000的所有商品的名称
select * from product where price <= 1000;
# 查询商品价格大于等于800的所有商品的名称和价格
select * from product where price >= 800;
# 查询商品价格不等于800的所有商品的信息
select * from product where price != 800;
select * from product where price <> 800;

三、范围查询

注意事项

  • batween and 查询连续范围内的数据,包含左右边界,是一个闭区间
  • between and必须从小到大值进行范围划分,否则没有数据
  • in 查询不连续区间的数据,in(数据1,数据2,...)

select 字段名 from 表名 where 字段名 in (数据1,数据2,数据3);

select 字段名 from 表名 where 字段名 between 数据1 and 数据2;

# 查询价格在200-1000范围内的所有商品,包含起始和终止位置,是一个闭区间[]
select * from product where price between 200 and 1000;
# 查询价格是200,800,2000,5000的所有商品信息
select * from product where price in (200,800,2000,5000);
# 查询产地是北京和南京的商品
select * from product where address in ('北京','南京');

四、逻辑查询

注意事项

  • and 逻辑与 同真即真
  • or 逻辑或 同假即假
  • not 逻辑非 非真即假,非假即真
# 查询价格在200-1000之间的所有数据,如果需要包含边界,则需要使用大于等于和小于等于
select * from product where price >= 200 and price <= 1000;
# 查询价格不在200-1000范围内的所有商品信息
select * from product where price < 200 or price > 1000;
select * from product where not (price >= 200 and price <= 1000);

五、模糊查询

注意事项

  • %:代表0个或多个字符
  • _:代表一个字符

select 字段名 from 表名 where 字段 like 规则;

# 查询地址以京结尾的所有商品
select * from product where address like '%京';
# 查询地址以北开头的所有商品
select * from product where address like '北%';
# 查询地址中带有南的所有商品
select * from product where address like '%南%';
# 查询地址是两个字的所有商品
select * from product where address like '__';
# 查询地址是三个字符,且结尾为江的商品信息
select * from product where address like '__江';

六、非空查询

注意事项

  • is null:判断是否为空
  • is not null:判断是否不为空

select 字段名 from 表名 where 字段名 is (not) null;

# 删除id=2的商品type的值
update product set type = null where id = 2;
# 查询所有商品中,type为空的字段
select * from product where type is null;
# 查询所有商品中type不为空的字段
select * from product where type is not null;
select * from product where not (type is null);
# 查询所有商品中type为空且name不为空的商品
select * from product where (type is null) and (name is not null);

七、排序查询

注意事项

  • order by:排序的关键字,可以构建指定字段,指定规则的排序
  • 如果按照多个字段进行排序,先按照排在前面的字段进行排序,如果排序值相同,则按照后边的规则排序
  • 如果需要升序排列,则不需要使用asc,默认就是升序排列
  • 按照多个字段规则进行排序,则先按照最前面的规则排序,如果排序过程中,值相同,则按照后边的字段进行排序
  • 文本类型排序规则
    • 没有数据 < 有数据
    • 排序按照编码表顺序排序,排在前面的小,排在后面的大。0-9递增,a-z递增,A-Z递增
    • 文本类型数据比较大小按位比较,第一位进行比较如果值大则大,值小则小,如果相同则比较第二位

select 字段名 from 表名 where 条件 order 字段名 排序规则(asc升序、desc降序);

# 查询所有商品中,价格大于1000的商品并按照价格进行降序排序
select * from product where price>1000 order by price desc;
# 按照type进行升序排列,如果type相同则按照价格进行降序排列
select * from product order by type asc ,price desc ;
# 按照价格进行降序排列,如果价格相同,则按照type进行升序排列
select * from product order by price desc ,type asc ;

八、分组查询

注意事项

  • group by:将记录按照指定字段分成多组,字段相同的内容分为一组
  • having
    • 在group by分组之后,不能使用where进行条件查询,只能通过having进行条件查询
    • 在having中可以使用聚合函数,但是在where中不可以使用
  • 在linux中默认开启了group by严格模式,在selet后边不能使用除了分组字段外的其他字段

select 分组字段/聚合函数/其他函数 from 表名 group by 分组字段 having 条件;

# 查询当前产品中每一类商品各有多少个
select type,COUNT(*) from product group by type;
# 查询当前商品中,每一类商品的平均价格是多少
select type,avg(price) from product group by type;
# 查询每一类商品的最大价格
select type,max(price) from product group by type;
# 查询商品种类超过两种的商品类型
select type,count(*) from product group by type having count(*) > 2;
select type,count(*) as c from product group by type having c > 2;
# 查询当前每一类商品的所有商品名称
select type,group_concat(name) from product group by type;

九、分页查询

注意事项

  • 分页查询就是价格查询到的数据按照一定的规则截取其中的一部分

select 字段名 from 表名 limit 开始索引的位置,要展示的条目数;

# 查询所有的商品数据,并展示前三条数据
select * from product limit 0,3;
# 如果从头开始展示,可以将起始位置进行省略
select * from product limit 3;
# 查询所有商品数据,并展示第5-7条数据
select * from product limit 4,3;

十、多表查询

注意事项

  • 内连接:交集,在查询过程中,保留左右两侧共有的记录
  • 左连接:差集,在查询过程中,保留左侧表全部数据,以及右侧表可以匹配到左表的数据
  • 右连接:差集,在查询过程中,保留右侧表全部数据,以及左侧表可以匹配到右侧表的数据
# 内连接:交集运算,左表中存在的数据,右表中也存在则被保存
select * from product inner join type on product.type = type.name;
# inner可以被省略
select * from product join type on product.type = type.name;
# 左外连接:左表中所有的数据都被保存下来,右表中只有能够匹配到左表的数据被保留
select * from product left outer join type on product.type = type.name;
# outer可以被省略
select * from product left join type on product.type = type.name;
# 右外连接:右表中所有的数据都被保存下来,左表中只有能够匹配到右表的数据被保留
select * from product right join type on product.type = type.name;
# 字段名称和表名,可以使用as关键字进行重命名字段名称
# 重命名之后,查询出来的内容的列名将会发生改变
select * from product as p right join type as t on p.type = t.name;
# as可以被省略
select * from product p right join type t on p.type = t.name;

十一、子查询

注意事项

  • 子查询就是select中嵌套一个select语句
  • select查询,可以作为一个值,一个数据序列,也可以作为一个表出现
  • 子查询可以单独执行,如果子查询单独执行出现报错,那放到其他语句中也一样会报错
# 获取所有商品的平均价格
select avg(price) from product;
# 获取所有商品的名称
select name from product;
# 获取所有商品的全部信息
select * from product;
# 获取产品价格大于平均价格的商品
select * from product where price > (select avg(price) from product);
# 获取和id = 1的商品价格相同的所有商品
select * from product where price = (select price from product where id = 1);
# 获取所有的数据,价格大于平均值,且id不等于10
select * from product where (price > (select avg(price) from product)) and id != 10;

十二、聚合查询

注意事项

  • 将整列数据进行聚合,计算成一个值的函数,如最大值,最小值,计数,求和,平均值等
  • count、sum、max、min、avg
# 获取所有商品的总数
# 使用count(*)的时候,必须当前记录中所有字段都为空时才能被忽略
select COUNT(*) from product;
# 使用聚合函数会忽略空值,在计算type的数量的时候没有统计空值
select COUNT(type) from product;
# 获取所有商品的总和
# sum求和忽略了空值
select sum(price) from product;
# 获取最贵商品的价格
select max(price) from product;
# 获取最便宜的商品的价格
select min(price) from product;
# 获取商品的平均价格
select avg(price) from product;
# 计算价格最大值与平均值的差,和最小值与平均值的差
select max(price)-avg(price),min(price)-avg(price) from product;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值