需要使用sql查询获取与内容和库表不同的计数
我有文件夹、内容、库表。每个文件夹在内容表和库表中都有大量内容。 内容和库表依赖于内容id。如果我们添加内容,它应该同时保存内容和库表。 突然,库表中一定发生了删除。库表中的某些内容已删除,但内容表中的部分内容未删除 我希望检索到特定的文件夹id、该文件夹中的内容数、该文件夹中的库数 我尝试了一些选项,可以检索内容表(而不是库表)中显示的文件夹id内容。使用单个查询检索文件夹\u id countcontent\u id content表、countcontent\u id库表需要使用sql查询获取与内容和库表不同的计数,sql,sql-server,Sql,Sql Server,我有文件夹、内容、库表。每个文件夹在内容表和库表中都有大量内容。 内容和库表依赖于内容id。如果我们添加内容,它应该同时保存内容和库表。 突然,库表中一定发生了删除。库表中的某些内容已删除,但内容表中的部分内容未删除 我希望检索到特定的文件夹id、该文件夹中的内容数、该文件夹中的库数 我尝试了一些选项,可以检索内容表(而不是库表)中显示的文件夹id内容。使用单个查询检索文件夹\u id countcontent\u id content表、countcontent\u id库表 select d
select distinct a.folder_id from (
select c.folder_id,c.content_id, l.content_id as lid from content c left join library l
on c.content_id = l.content_id
where l.content_id is null) a
结果:
folder_id ccount lcount countdiff
123 33 30 3
也许是这样的
select
a.*,
ccount-lcount as diff
from
(
select
folder_id,
(select count(*) from content c where c.folder_id=f.folder_id) ccount,
(select count(*) from library l where l.folder_id=f.folder_id) lcount
from
folders
) a
结果查询:选择一个*,从“选择文件夹\u id”中将lcount作为diff计算,从“选择文件夹路径”,从“内容c”中选择“计数”,其中c.folder\u id=f.folder\u id帐户,从库l中选择计数,其中l.parent\u id=f.folder\u id l从content\u folder\u id=li.content\u id,li.content\u id作为lid从content ct左连接库li在ct.content\u id=li.content\u id其中li.content\u id为null