日期:2014-05-19  浏览次数:20632 次

急求助一条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