1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > mysql查询嵌套where_MySQL-10(where /from 嵌套查询)

mysql查询嵌套where_MySQL-10(where /from 嵌套查询)

时间:2021-11-24 20:40:34

相关推荐

mysql查询嵌套where_MySQL-10(where /from 嵌套查询)

# 引出: 按照网站,取出Product_id 最大的

select product_name,product_price,product_id

from tb_name

order by product-id

limit 0,1;

思考: 在这里是利用 limit 语句来实现,再考虑用子查询来实现

# 这里考虑分两步来实现

1.先使用max聚合找到 product_id 最大的值

即:

select max(product_id), product_name, product_price

from tb_name

2.再使用 where product-id = ‘max中的最大值’,假设max_product_id = 33;

即:

select product_id, product_name, product_price

from tb_name

where product_id = 33;

综合 1,2 两步

即:

select product_id ,product_name, product_price

from tb_name

where product =(select max(product_id),product_name,product_price from tb_name )

到此:我们就引出了子查询的概念

where 子查询,即将内层查询的结果作为外层查询的 where 条件值

强化:

将tb_name 中,取出每个cat_id 下 product_id 最大的商品信息

第一:

select max(product_id),product_name

from tb_name

group by cat_id; # 这里先按照cat_id 取出每个栏目下最大的product_id

第二:

select product_id ,product_price ,cat_id

from tb_name

where product in ( select max(product_id,product_name,product_price

from tb_name

group by cat_id));

# from 子查询

select * from (

select product_id ,product _name,product_price

from tb_name

order by cat_id desc,

product_price asc)

as temp

group by cat_id;

mark: 正月初六

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。