首页 > 数据库技术 > 详细

mysql查询

时间:2015-07-07 11:12:42      阅读:251      评论:0      收藏:0      [点我收藏+]

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查询

原文:http://my.oschina.net/everyDay111/blog/475537

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!