mysql学习笔记2:查询语句, 子查询

本文详细介绍了SQL查询的基础知识,包括各种运算符的使用、模糊查询、统计函数、分组和排序等高级特性,帮助读者掌握高效的数据检索方法。

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

目录

 

查询语句

where子句

1.比较运算符

2 逻辑运算符

3模糊查询 使用通配符like

4查询模型 计算字段与列别名

练习题:

group by子句与统计函数

1统计函数

2 分组子句

having 子句

having针对结果集操作。而where针对表操作

having和group的综合使用

练习题:

order by子句

limit 子句

练习题:

子查询

where型子查询:

from型子查询

exists型子查询

奇怪的null


查询语句

where子句

1.比较运算符

查询主键id值为32的商品
select  goods_id,goods_name,shop_price from goods where goods_id=32;


查询cat_id!=3的
select  goods_id,cat-id,goods_name,shop_price from goods where cat_id!=3;


查询本店价格高于3000元的商品
select  goods_id,goods_name,shop_price from goods where shop_price>3000;


查询本店价格小于等于100元的商品
select  goods_id,goods_name,shop_price from goods where shop_price<=100;


查询第4栏目和11栏目的商品(不用or)
select  goods_id,cat-id,goods_name from goods where cat_id  in  (4,11);


查询商品价格范围为100到500之间的商品
select  goods_id,goods_name,shop_price from goods where shop_price between 100 and 500;

2 逻辑运算符

取出不在第三个栏目也不在11栏目的其余商品(用两种方式not in 与and实现)
select goods_id,cat_id,goods_name from goods where cat_id not in(3,11);

select goods_id,cat_id,goods_name from goods where cat_id!=3  and  cat_id!=11;


选出价格在100-300或者4000-5000的商品
select goods_id,goods_name ,shop_price from goods where (shop_price>=100 and shop_price<=300) or (shop_price>=4000 and shop_price<=5000);


取出第三个栏目下价格小于1000或大于3000,同时点击量>=5.
select goods_id,cat_id,goods_name,shop_price  click_count from goods where cat_id=3 and (shop_price>3000 or shop_price<1000) and click_count>=5;

3模糊查询 使用通配符like

查出诺基亚开头的所有商品
 select goods_id,goods_name,shop_price from goods where goods_name like '诺基亚%';


匹配单个字符的查询。下划线
select goods_id,goods_name,shop_price from goods where goods_name like '诺基亚N__';

4查询模型 计算字段与列别名

查询本店价格比市场价格便宜超过200的所有商品
select goods_id,goodS_name,(market_price-shop_price) as discount
from goods
where (market_price-shop_price)>200;

where子句对数据表中的数据起作用,查询出来数据。不是对查询结果发挥作用。

如果要对查询结果进行筛选,应该使用having子句

列就是变量,在每一行上,列的值都在变化

where条件是表达式,哪一行上为真,就读取出来。

 

练习题:

给表和数组,把num值在[20,29] 改为20,[30,39]的改为30。        num:3  12  15  25   23   29    34   37   32   45  48   53 

思路:把num看为变量,num/10再取整,然后乘10

update mian set num=floor(num/10)*10 where num>=20 and num<=39;

截取字符串和拼接字符串的函数

select goods_id,goods_name,concat('htc',substring(goods_name,4)) from goods where goods_name like '诺基亚%';

group by子句与统计函数

1统计函数

查询最贵的商品的价格
select max(shop_price) from goods; 

查询最便宜的商品的价格
select min(shop_price) from goods; 

查询最旧(最小)的商品编号
select min(goods_id) from goods; 

统计商品的库存量
 select sum(goods_num) from goods;

查看所有商品的平均价格
select avg(shop_price) from goods;

统计商品一共有多少种.*计数所有行,null行也计数。而count(列名)只计数不是null的行
select count(*) from goods;

使用count(*)还是count(列名)?

其实对于myisam引擎的表没有区别,这个引擎内部有一个计数器在维护行数

innodb的表,count(*)读取函数,效率很低。因为innodb真的去数一遍函数

2 分组子句

统计函数单独使用意义不大,要和分组配合使用

计算第三个栏目下所有商品的库存量
select sum(goods_num) from goods where cat_id=3;

统计每一个栏目下的库存量:分了组,然后再统计每一组。
select cat_id,sum(goods_num) from goods group by cat_id;


不是一条标准的sql语句,是mysql的特有的,可以这么干。goods_id把第一次出现的值取出来。出于可移植性和规范性,不推荐这么写。
select goods_id,sum(goods_num) from goods;


按cat_id分组,计算每个栏目的平均价格
select cat_id,avg(shop_price) from goods group by cat_id;

严格的说,group by 的a,b,c列时,select 的列只能在a,b,c中选择。

having 子句

having针对结果集操作。而where针对表操作

查询本店价格比市场价格便宜超过200的所有商品.使用having筛选结果集
select goods_id,goodS_name,(market_price-shop_price) as discount
from goods
having discount>200;

having和group的综合使用

查询每种商品积压的货款(就是库存的商品值多少钱)
select goods_id,shop_price,goods_num,shop_price*goods_num as money  from goods;


 查询该店积压的总货款
select sum(shop_price*goods_num) from goods;

查询每个栏目里面积压的货款
select cat_id,sum(shop_price*goods_num) from goods group by cat_id;


查询积压的货款大于2万的栏目,以及对应积压的货款金额
select cat_id,sum(shop_price*goods_num) as tmoney from goods group by cat_id  having  tmoney>20000;

查询本店价格比市场价格省的钱,而且剩的钱大于200的商品
select goods_id,market_price,shop_price,(market_price-shop_price) as discount from goods having discount>200;

练习题:

学生成绩表的语数外政史地,查询两门及两门以上不及格者的平均成绩

 先计算所有人的平均分
select name,avg(score) from result group by name;

再计算每个人的挂科情况. 挂科数就是sum(g)
select name,subject,score,score<60 as g from result; 

综合起来
select name,avg(score),sum(score<60) as gks from result group by name having gks>=2;



还有正常的思路: 使用子查询 先找挂科数大于等于2的人,

select name  from (select name,count(1) as gks from result where score<60 group by name having gks>=2) as tmp;  

 
子查询:
select name,avg(score) from result where name in (

select name  from (select name,count(1) as gks from result where score<60 group by name having gks>=2) as tmp;

) group by name;

 

order by子句

在结果集出来之后可以进行排序,排序语句要在having语句之后,顺序不能乱。

语法:order by 结果集中的列名  desc/asc

多字段排序的语法 order by 列1 desc/asc,列2 desc/asc , …………, 列n desc/asc;

取出第4个栏目下的商品,并按照从高到低排序
select goods_id,goods_name,shop_price 
from goods
where cat-id=4
order by shop_price  desc; 


把栏目4下的商品按照发布时间升序排列
select goods_id,goods_name,shop_price
from goods
where cat_id=4
order by add_name asc;


按照栏目升序排序,在一个栏目下的商品按照价格降序排序
selcet goods_id,cat_id,goods_name,shop_price
from goods
order by cat_id asc,shop_price desc;

 

limit 子句

限制条目

limit [offset,] N;

offset:表示偏移量,跳过前多少行。可以不写,不写就相当于0.

N:表示取出多少条目。

限制条目
select goods_id,goods_name,shop_price
from goods
where cat_id=3
order by shop_price asc
limit 10;


查出本店价格最高的前三名
select goods_id,goods_name,shop_price
from goods
order by shop_price desc
limit 0,3;


查出本店价格最高的前三名 到前五名
select goods_id,goods_name,shop_price
from goods
order by shop_price desc
limit 2,3;

取出价格最高的商品
select goods_id,goods_name,shop_price
from goods
order by shop_price desc
limit 1;

 

练习题:

查出每个栏目下id号最大的商品,就是每个栏目下最新的商品

select goods_id,cat_id,goods_name
from goods
order by cat_id asc,goods_id desc;


select goods_id,cat_id,goods_name
from (
select goods_id,cat_id,goods_name
from goods
order by cat_id asc,goods_id dess;
) as temp
gruop by cat_id;

 

子查询

where型子查询:

内层查询的结果,当成外层查询的条件来使用.

查出网店最新的一个商品,就是goods_id最大的
select goods_id,goods_name,shop_price
from goods
order by goods_id desc
limit 1;

不使用排序来查出网店最新的一件商品,就是goods_id最大的
select goods_id,goods_name
from goods
where goods_id=(select max(goods_id) from goods);



查询每个栏目下id号最大的商品

select max(goods_id) from goods group by cat_id;


select goods_id,cat_id,goods_name 
from goods
where goods_id in (

select max(goods_id) from goods group by cat_id;
)
group by cat_id;

from型子查询

查询结果集在结构上可以当作表来用

内层的查询的结果集,当作一张临时的表,让外层的语句来查询

查询每一个栏目下的最新的商品
select goods_id,cat_id,goods_name
from goods
order by cat_id asc,goods_id desc;



select *
from(

select goods_id,cat_id,goods_name
from goods
order by cat_id asc,goods_id desc;
)  as temp

group by cat_id;

 

exists型子查询

把外层sql的结果,拿到内层sql进行测试。如果成立,则改行取出来

查出有商品的栏目
 需要两张表


select cat_id,cat_name 
from category
where exists (select * from goods where goods.cat_id=category.cat_id);

 

奇怪的null

为什么最好不让列的值为null?

不好比较,效率不高,影响索引的效率

null是空的,比较时只能由两种运算符比较,其他运算符一律返回null。

因此在建表时避免选择null。

 

null是空,没有。比较null要使用特殊的运算符 is null  或者  is not null

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值