现在的位置: 首页 > 综合 > 正文

一个sql语句的思考:select count(*) from table where filedName0=value group by filedName1

2012年08月28日 ⁄ 综合 ⁄ 共 777字 ⁄ 字号 评论关闭

有如下表:table
-----------------------------------
Filed0                  Filed1
-----------------------------------
a                          1
a                          2
a                          2
b                          2
b                          3
b                          3
------------------------------------
执行:select Filed0,count(*) from table where Filed1>2 group by Filed0
得到结果:
------------------------------------
b                          2
------------------------------------
执行:(select Filed0,count(*) from table where Filed1>2 group by Filed0) union (select Filed0,0 from table where Filed0 not in (select Filed0 from table where Filed1>2) group by Filed0) order by Filed0
得到结果:
------------------------------------
a                          0
b                          2
------------------------------------
 

抱歉!评论已关闭.