有个表
表结构:
id 自增
goods_id 商品id
price 价格
现有需求是查询该表价格最低的数据,要求goods_id查询出来是唯一的并且id和查询出来的price是对应的。请问怎么写mysql 语句
看看我这个是不是你想要的,rank也可以替换成dense_rank
select id,goods_id,price from(
select id,goods_id,price,
rank() over(order by price) col
from tempTable
) u where col=1;
不知道是不是这个意思,可以查询结果试试看
select *
from table a
where (a.goods_id, a.price) in (SELECT * from ( select b.goods_id goods_id, min(b.price) price
from table b
group by b.goods_id
having count(1) = 1 ORDER BY min(b.price) LIMIT 1 ) temp );
一个表吗?
select goods_id,min(price) from 表名 group by goods_id
SELECT id, goods_id, MIN(price) AS price FROM 表名 GROUP BY goods_id
-- 以商品id进行分组,就是唯一的记录
select goods_id,min(price) price from testData group by goods_id