以前写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唯一,所以返回的还是一个值。实质没有变。