超长sql语句的用法总结

   以前写sql语句从来不考虑效率,如今要改变这个想法了。

 

       数据库的记录都是几百万条数据呀。

select c.name ,a.截止到2008年12月31号,b.截止到2009年3月31号 from city_code  c left join 
 (select g.cityid as cid, count(g.cityid) 截止到2008年12月31号 from group_info g where
 g.typeid=5
 and g.regtime<=to_date('20081231','yyyymmdd')
 group by g.cityid)
 a 
on a.cid=c.nid
left join 
 (select g.cityid as cid, count(g.cityid) 截止到2009年3月31号 from group_info g where
 g.typeid=5
 and g.regtime<=to_date('20090331','yyyymmdd')
 group by g.cityid)
 b 
on b.cid=c.nid

 

注意 a的前面不能用as,b的前面也不能用。

别名不能太长也不能打引号。

group_info表数据量很大,而city_code是和码表,所以要选择它为基准表。

to_date方法返回date类型。结果如下:

	武汉	9658	10445
	襄樊	1051	2310
	鄂州	688	732
	孝感	1733	1749
	黄冈	2651	4145
	黄石	1136	1500
	咸宁	2120	2121
	荆州	2112	2511	
                宜昌		2184
	恩施	719	719
	十堰	1010	1088
	随州	752	988
	荆门	729	729
	仙桃	1533	1566

 

 

更新:

 

 

update card_info ci
     set ci.status     = 3,
         ci.statusdate = (select distinct act_date
                            from tempc_active_card ai
                           where ai.cardno = ci.cardno)
   where ci.status = 0
     and ci.cardno in (select cardno from tempc_active_card);
     

 

这里面的更新很有意思,set   ci.statusdate 的值,以往我们总是给它个常量,在这里我们给的是

一个符合条件的值,cardno唯一,所以返回的还是一个值。实质没有变。

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值