时间:2021-07-01 10:21:17 帮助过:2人阅读
1、sql中遇到聚集函数,需要单独查询
当我想查询表中最早创建的记录,最开始我是这样写的
select * from (select *,min(temp.gmt_create)as min_create_date from (select *,(select merchant_id from shop.shop ss where ss.id = zc.shop_id) as merchant_id from zforce.contract zc where deleted = 0)temp where merchant_id = (select merchant_id from shop.shop ss where ss.id = 35)) temp2 where min_create_date = gmt_create;
但是后来发现如果如果查询项中有min()函数,只会返回一行。
需要单独查询min(),这样构造虚表就可以了
select * from (select *, (select merchant_id from shop.shop ss where ss.id = zc.shop_id) as merchant_id, (select min(gmt_create) from zforce.contract)as min_create_date from zforce.contract zc where deleted = 0)temp where merchant_id = (select merchant_id from shop.shop ss where ss.id = 35) and gmt_create=min_create_date
mysql查询
标签: