首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

急一条sql语句!

2012-02-06 
急求助一条sql语句!!两个表table1table1idnamestr1str21名字1122名字2233名字324.......table2table2idt1i

急求助一条sql语句!!
两个表
table1
table1id     name     str1     str2
1                 名字1       1           2
2                 名字2       2           3
3                 名字3       2           4
.......

table2
table2id       t1id       info
1                         1                 null
2                         1                   11
3                         1                 null
4                         2                   123
5                         2                 null
6                         2                   233
..........
table1的id对应的是table2的t1id
要的列表是符合条件的table2中的info为空的记录数量,条件为table1中的str1=2   or   str2=2

上面的结果应该是
name             为空的记录数量      
名字1               2
名字2               1

这样的语句应该怎么写??


[解决办法]
select b.name,count(1)
from table2 a
left join table1 b on a.id = b.t1id
where a.info is null
group by b.name
[解决办法]
select a.name,为空的记录 = (select count(b.info) from table2 b where b.info is null and b.t1id = a.id ) from table1 a where a.str1=2 or a.str2=2
这样呢
[解决办法]
select name,count(*) as 为空的记录数量 from

table1 a inner join table2 b on a.table1id = b.t1id where b.info is null and

(a.str1=2 or a.str2=2)

group by name

热点排行
Bad Request.