假设我有一个表 tableA,结构如下:id int 自增
info varchar(50)
type1 int type1 只有 2 种值,分别为 1,2
type2 int type2 只有 2 种值,分别为 3,4
tcount int我想这样进行查询,输入一个字符串 query,假设这个 query 是“古天乐”,select count(*) from A where info='古天乐' 的记录条数总共为200条,其中: select count(*) from A where info='古天乐' and type1 = 1 的记录条数为100条,
select count(*) from A where info='古天乐' and type1 = 2 的记录条数为100条,
select count(*) from A where info='古天乐' and type1 = 1 and type2 = 3 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 1 and type2 = 4 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 2 and type2 = 3 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 2 and type2 = 4 的记录数为50条,想得到一个这样的查询结果,这个结果总共只有两条记录,括号内为解释:字段名: 古天乐(字段名为query的值,但对应的记录是type1的值) 总记录数 记录数1 记录数2
字段意思:
第一条记录: 1 100(即type1=1) 50(即type1=1,type2=3) 50(即type1=1,type2=4)
第二条记录: 2 100(即type1=2) 50(即type1=2,type2=3) 50(即type1=2,typ2=4)请问能用一条SQL语句解决么?
info varchar(50)
type1 int type1 只有 2 种值,分别为 1,2
type2 int type2 只有 2 种值,分别为 3,4
tcount int我想这样进行查询,输入一个字符串 query,假设这个 query 是“古天乐”,select count(*) from A where info='古天乐' 的记录条数总共为200条,其中: select count(*) from A where info='古天乐' and type1 = 1 的记录条数为100条,
select count(*) from A where info='古天乐' and type1 = 2 的记录条数为100条,
select count(*) from A where info='古天乐' and type1 = 1 and type2 = 3 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 1 and type2 = 4 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 2 and type2 = 3 的记录数为50条,
select count(*) from A where info='古天乐' and type1 = 2 and type2 = 4 的记录数为50条,想得到一个这样的查询结果,这个结果总共只有两条记录,括号内为解释:字段名: 古天乐(字段名为query的值,但对应的记录是type1的值) 总记录数 记录数1 记录数2
字段意思:
第一条记录: 1 100(即type1=1) 50(即type1=1,type2=3) 50(即type1=1,type2=4)
第二条记录: 2 100(即type1=2) 50(即type1=2,type2=3) 50(即type1=2,typ2=4)请问能用一条SQL语句解决么?
第一条记录: 1,100(即type1=1),50(即type1=1,type2=3),50(即type1=1,type2=4)
第二条记录: 2,100(即type1=2),50(即type1=2,type2=3),50(即type1=2,type2=4)
select 1 as type,count(1) as cnt1,sum(case when type2=3 then 1 else 0 end) as cnt2,sum(case when type2=4 then 1 else 0 end) as cnt3 from tb where info='古天乐' and type1=1
union all
select 2,count(1) as cnt1,sum(case when type2=3 then 1 else 0 end) as cnt2,sum(case when type2=4 then 1 else 0 end) as cnt3 from tb where info='古天乐' and type1=2
, count(1)
, count(case type2 when 1 then 1 end)
, count(case type2 when 2 then 1 end)
from A
where info=@q
group by type1
, count(1)
, count(case type2 when 3 then 1 end)
, count(case type2 when 4 then 1 end)
from A
where info=@q
group by type1
SELECT COUNT(1) AS [总记录数],
SUM(CASE WHEN type2 = 3 THEN 1 ELSE 0 END) AS [记录数1],
SUM(CASE WHEN type2 = 4 THEN 1 ELSE 0 END) AS [记录数2]
FROM A
WHERE info='古天乐'
AND type1 = 1
UNION
SELECT COUNT(1) AS [总记录数],
SUM(CASE WHEN type2 = 3 THEN 1 ELSE 0 END) AS [记录数1],
SUM(CASE WHEN type2 = 4 THEN 1 ELSE 0 END) AS [记录数2]
FROM A
WHERE info='古天乐'
AND type1 = 2
select count(1),(select count(1) from tablename where type2=3),(select count(1) from tablename where type2=4) from tableName where info=@name
group by type1
--author:josy
--editor:galenkeny
select 1 as type,
count(1) as cnt1,
sum(case when type2=3 then 1 else 0 end) as cnt2,
sum(case when type2=4 then 1 else 0 end) as cnt3
from tb
where info='古天乐' and type1=1union allselect 2 as type,
count(1) as cnt1,
sum(case when type2=3 then 1 else 0 end) as cnt2,
sum(case when type2=4 then 1 else 0 end) as cnt3
from tb
where info='古天乐' and type1=2