mysql查询嵌套where_MySQL-10(where /from 嵌套查询)
# 引出: 按照網(wǎng)站,取出Product_id 最大的
select product_name,product_price,product_id
from tb_name
order by product-id
limit 0,1;
思考: 在這里是利用 limit 語句來實現(xiàn),再考慮用子查詢來實現(xiàn)
#? 這里考慮分兩步來實現(xiàn)
1.先使用max聚合找到 product_id 最大的值
即:
select max(product_id), product_name, product_price
from tb_name
2.再使用 where product-id = ‘max中的最大值’,假設(shè)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 子查詢,即將內(nèi)層查詢的結(jié)果作為外層查詢的? 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: 2020年正月初六
總結(jié)
以上是生活随笔為你收集整理的mysql查询嵌套where_MySQL-10(where /from 嵌套查询)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 清空mysql注册表步骤_完全卸载MyS
- 下一篇: java 字符串去掉换行_java第一个