2013-10-24 65 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