现在有这个么一张表:
table test
id , name,type,dc这几个属性
数据有下面一条
id name type dc
1 a qw 12
2 b w 9
3 c qw 12
4 d w 9
5 e w 9
6 f w 9
7 g w 11
现在我想通过sql语句查询
合计 dc12 dc9 dc其他
heji type(qw) type(w) heji type(qw) type(w) heji type(qw) type(w)
7 2 1 1 4 1 3 1 1
请问一下这样的sql怎么写??
...全文
51015打赏收藏
一条sql怎么写
现在有这个么一张表: table test id , name,type,dc这几个属性 数据有下面一条 id name type dc 1 a qw 12 2 b w 9 3 c qw 12 4 d w 9 5 e w 9 6 f w 9 7 g w 11 现在我想通过sql语句查询 合计 dc12 dc9 dc其他 heji type(qw) type(w) heji type(qw) type(w) heji type(qw) type(w) 7 2 1 1 4 1 3 1 1 请问一下这
select
distinct dc,
count(*) heji,
(select count(*) from test t1 where t1.dc = t.dc and t1.type = 'qw') type(qw),
(select count(*) from test t1 where t1.dc = t.dc and t1.type = 'w') type(w)
from test t group by dc
能不能嵌套几条sql来处理下,纯后台出里,是不是速度效率方面有点差[/quote]
嵌套sql?不知道怎么写能出来你要的效果。 而且如果sql很复杂,效率也不会太高。 不过可以写两个查询sql,其中一个sql,
select id,name,type,case when dc in(12,9) then dc else -999 end from test 这样查出来12,9和其他的结果。再用一个sql
select * from (select id,name,type,case when dc in(12,9) then dc else -999 end from test ) 这样就可以不用在程序中判断是不是等于12或者9了,而且通过这样的sql 查出来的结果不会有很多行吧? 后台调用循环肯定超级快。而且我感觉即便是不这样写,后台执行代码也会很快,效率问题可以不用考虑。 [/quote]
谢谢哦
能不能嵌套几条sql来处理下,纯后台出里,是不是速度效率方面有点差[/quote]
嵌套sql?不知道怎么写能出来你要的效果。 而且如果sql很复杂,效率也不会太高。 不过可以写两个查询sql,其中一个sql,
select id,name,type,case when dc in(12,9) then dc else -999 end from test 这样查出来12,9和其他的结果。再用一个sql
select * from (select id,name,type,case when dc in(12,9) then dc else -999 end from test ) 这样就可以不用在程序中判断是不是等于12或者9了,而且通过这样的sql 查出来的结果不会有很多行吧? 后台调用循环肯定超级快。而且我感觉即便是不这样写,后台执行代码也会很快,效率问题可以不用考虑。