2013-10-24 68 views
0

我有文件夹,内容库表从内容和库表不同的计数。每个文件夹在内容表和库表中都有很多内容。 内容和库表依赖于content_id。如果我们添加内容,它应该保存内容和库表。 Sudendly必须发生在图书馆的桌子删除。一些内容在库表中被删除,但不在内容表中被删除。需要获得使用SQL查询

我想要检索的特定文件夹的ID,该文件夹中的内容的数量,图书馆的计数文件夹中。

我尝试一些选择,我可以能够检索文件夹ID(在库表中内容表未呈现的内容)。使用单查询检索folder_id计数(的content_id内容表),计数(的content_id库表)

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 

回答

0

也许这样的事情?

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 
+0

哇,谢谢你这么多。 – user2739544

+0

结果查询:从内容c其中c.folder_id = f.folder_id)ccount, 选择 的a *, ccount-lcount作为DIFF 从 ( 选择 folder_id,FOLDERPATH, (SELECT COUNT(*)(。从库l中选择count(*),其中l.parent_id = f.folder_id)lcount from content_folder_tbl f其中folder_id in(从( 中选择不同的al.folder_id)选择ct.folder_id,ct.content_id,li.content_id作为lid内容CT左加入库利 上ct.content_id = li.content_id 其中li.content_id为null)人) )一 – user2739544