mysql数据库单表查询教程

本文详细介绍了如何使用SQL进行单表查询,包括选择显示的列、去除重复数据、使用where条件、like模糊查询、in范围查询、order by排序、聚合函数及group by分组等操作,帮助读者掌握MySQL数据库的基础查询技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

select [distinct] [*] [列名1,列名2] from 表名 [where 条件]

distinct 去除重复的数据

 

select 选择显示哪些列的内容

 

--商品分类: 手机数码,鞋靴箱包

1.分类的ID

2.分类名称

3.分类描述

1,2

 

insert into category values(null,'手机数码','电子产品,黑马生产');

insert into category values(null,'鞋靴箱包','江南皮鞋厂倾情打造');

insert into category values(null,'香烟酒水','黄鹤楼,茅台,二锅头');

insert into category values(null,'酸奶饼干','娃哈哈,蒙牛酸酸乳');

insert into category values(null,'馋嘴零食','瓜子花生,八宝粥,辣条');

 

--所有商品

1.商品ID

2.商品名称

3.商品的价格

4.生产 日期

5.商品分类ID

 

商品和商品分类:所属关系

insert into product values(null,'小米mix4',998,null,1);

insert into product values(null,'锤子',2888,null,1);

insert into product values(null,'阿迪王',99,null,2);

insert into product values(null,'老村长',88,null,3);

insert into product values(null,'劲酒',35,null,3);

insert into product values(null,'小熊饼干',1,null,4);

insert into product values(null,'卫龙辣条',1,null,5);

insert into product values(null,'旺旺饼干',1,null,5);

 

--简单查询

--查询所有的商品

select * from product;

--查询商品名称和商品价格

select pname,price from product;

 

---别名查询,as关键字,as关键字是可以省略

--表别名: select p.pname,p.price from product p; (主要是用在多表查询)

select p.pname,p.price from product as p;

--列别名: select pname as 商品名称, price as 商品价格 from product;

select pname as 商品名称,price as 商品价格 from product;

省略as关键字

select pname 商品名称,price 商品价格 from product;

 

---去掉重复的值

--查询商品所有的价格

select price from product;

select distinct price from product;

 

--select运算查询 : 仅仅在查询结果上做了运算 + - * /

select *,price*1.5 from product;

select *,price*1.5 as 折扣价 from product;

 

--条件查询 [where关键字]

指定条件,确定要操作的记录

 

--查询商品价格>60元的所有商品信息

select * from product where price > 60;

 

-- where 后的条件写法

  • 关系运算符: > >= < <= = != <>

<> : 不等于 : 标准SQL语法

!= : 不等于 : 非标准SQL语法

查询商品价格不等于88的所有商品

select * from product where price <> 88;

select * from product where price != 88;

查询商品价格在10到100之间

select * from product where price > 10 and price < 100;

between...and...

select * from product where price between 10 and 100;

--逻辑运算: and ,or,not

--查询出商品价格 小于35 或者商品价格 大于900

select * from product where price < 100 or price > 900;

--like : 模糊查询

_ : 代表的是一个字符

% : 代表的是多个字符

--查询出名字中带有饼的所有商品

select * from product where pname like '%饼%';

--查询第二个名字是熊的所有商品 '_熊'

select * from product where pname like '_熊%'

-- in 在某个范围中获得值

--查询出商品分类ID在 1,4,5里面的所有商品

select * from product where cno in (1,4,5);

 

--排序查询: order by 关键字

asc: ascend 升序 (默认的排序方式)

desc: descend 降序

 

--0. 查询所有商品,按照价格进行排序

select * from product order by price;

--1.查询所有的商品,按价格进行降序排序(asc-升序 desc-降序);

select * from product order by price desc;

--2.查询名称有 小 的商品,按价格升序排序

--查询名称有小的商品

select * from product where pname like '%小%';

--进行排序得出结果

select * from product where pname like '%小%' order by price asc;

--聚合函数:

sum() : 求和

avg() : 求平均值

count(): 统计数量

max(): 最大值

min(): 最小值

--1.获得所有商品价格的总和

select sum(price) from product;

--2.获得所有商品的平均价格

select avg(price) from product;

--3.获得所有商品的个数

select count(pid) from product;

 

--注意: where条件后面不能接聚合函数

 

--分组: group by

--1.根据cno字段分组,分组后统计商品的个数

select cno,count(*) from product group by cno;

--2.根据cno分组,分组统计每组商品的平均价格 并且商品平均价格 > 60

select cno,avg(price) from product group by cno having avg(price) > 60;

--having 关键字 可以接聚合函数的 出现在分组之后

--where 关键字 它是不可以接聚合函数 出现在分组之前

--填写顺序

select ... from ... where ... group by ... having ... order by

--执行顺序

from ... where ... group by ... having ... select ... order by

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值