请教一个SQL查询~

crescent_star crescent_star 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
假设我有一个表 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语句解决么?
...全文
评论 12
收藏 已收藏
分享
返回顶部
crescent_star
MS-SQL Server 34,876 成员 254,635 内容