请教一个SQL查询~
假设我有一个表 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语句解决么?
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语句解决么?
...全文
相关推荐
sql基础~查询.sql
sql基础~查询.sql
SQL查询语句精华~
,即嵌套的SQL查询,使查询更为复杂和灵活。
50道SQL练习题及答案与详细分析(01~05)
50道SQL练习题及答案与详细分析(题目和数据初始化)50道SQL练习题及答案与详细分析(01~05)50道SQL练习题及答案与详细分析(06~10)1.查询”01″课程比”02″课程成绩高的学生的信
sql查询分析器非常好用
sql查询分析器,网上下的基本不能用,我这个很好用哦~
SQL查询分析器分离版本
SQL查询分析器分离版本,挺好用的~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


