sql语句求解
有两个表
表一:
note_id noteCategory_id
1 1
2 2
3 1
4 1
5 2
表二:
noteCategory_id noteCategoryName
1 bank
2 government
求sql语句或存储过程:
得到:
noteCategory_id noteCategoryName num
1 bank 3
2 government 2
num为noteCategory 所具有的note_id的数量,从表一中得到 的。
谢了哦
------解决方案--------------------select a.*,b.num from (select noteCategory_id,count(*) num from 表一 group by noteCategory_id)a,表二 b where a.noteCategory_id=b.noteCategory_id
------解决方案--------------------select a.noteCategory_id,b.noteCategoryName,count(1)
from table1 a
left join table2 b on a.noteCategory_id=b.noteCategory_id
gourp by a.noteCategory_id,b.noteCategoryName
------解决方案--------------------select a.noteCategory_id,a.noteCategoryName,count(b.noteCategory_id)num
from 表二 a left join 表一 b on a.noteCategory_id=b.noteCategory_id
group by a.noteCategory_id,a.noteCategoryName
noteCategory_id noteCategoryName num
--------------- -------------------- -----------
1 bank 3
2 government 2
(所影响的行数为 2 行)
------解决方案-------------------- select
T.noteCategory_id,
T.noteCategoryName,
(select count(*) from 表一 where noteCategory_id=T.noteCategory_id ) as num
from 表二 AS T