一个sql语句的思考:select count(*) from table where filedName0=value group by filedName1
2007-06-06 09:02
531 查看
有如下表: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
------------------------------------
-----------------------------------
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
------------------------------------
相关文章推荐
- LINQ体验(5)——LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- mybatis一个select标签执行多个sql语句
- 用到 count; group by ; order by ;having 的一个SQL语句
- 在同一个sql语句中,统计不同条件的Count数量
- sql语句 自己编写一个函数reversion,完成颠倒一个字符串,即:select dbo.reversion('abcd') 输出的结果为 dcba
- 一个批量更新的sql语句引发的关于创业者心态的思考
- LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- SQL 从一个sql 语句结果中(作为AS一个表) 查询结果 ;按照count排序
- LINQ体验(5)——LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- 【SQL】在含有GROUP BY的SELECT语句中如何显示COUNT()为0的结果
- 在同一个sql语句中如何写不同条件的count数量
- LINQ体验(5)——LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- 学习 SQL 语句 - Select(7): 分组统计之 Avg()、Sum()、Max()、Min()、Count()
- sql 语句 写一个函数T-sql函数elimination(a,b)将出现在第一个字符串中的第二个字符串中的所有字符删除,即:select dbo.elimination('abcdefg123
- LINQ体验(5)——LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- 在同一个sql语句中如何写不同条件的count数量 (转)
- LINQ体验(5)——LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg
- linq to sql 语句基本查询(3):Select和Count/Sum/Min/Max/Avg
- 转>>在同一个sql语句中如何写不同条件的count数量
- select语句的结果集直接发向客户端的(一个SQL语句会前后进行各种调用,包括解析(parse)调用、执行(executive)调用、获取(fetch)调用)