2015-09-26 220 views
0

考慮這三個表:使用聯接涉及視圖和表返回更多的記錄比預期

內容:

Id(pk), Name, and ContentTypeId(fk from ContentType) 

ContentTypeId:

此表將有值如

1文本

2個

...

Id(pk), Value 

ContentUpdates:

Id(fk from Content), Status, UpdateDate 

所有三個爲報表獲取數據有一種觀點像這樣:

select C.Id, C.Name, CT.Value, CU.UpdateDate 
from ContentUpdates CU 
join Content C 
    On CU.Id = C.Id 
join ContentType CT 
    On C.ContentTypeId = CT.Id; 

目前這個特定的視圖給了我800條記錄。

我在哪裏,我需要所有這些列類似的要求加上一個過濾器基於在ContentUpdates表中的狀態列

我想用這個視圖上的ID加入「ContentUpdates」表。

例如,我可能想要獲取狀態爲5的所有內容。 當前只有2條記錄具有該狀態,但是在視圖上使用JOIN時,結果集的記錄方式比這更多。

我在做什麼錯?

可以查看實際使用還是我更好用這樣的:

SELECT C.Id, C.Name, CT.Value, CU.UpdateDate 
FROM ContentUpdates CU 
JOIN Content C 
    On CS.Id = C.Id 
JOIN ContentType CT 
    On C.ContentTypeId = CT.Id 
WHERE CU.Status = 5; 

回答

0

是的,你可以使用一個觀點,但你需要公開任何你想要的或顯示查詢字段:

CREATE VIEW content_view 
AS 
SELECT C.Id, C.Name, CT.Value, CU.UpdateDate, CU.Status 
FROM ContentUpdates CU 
INNER JOIN Content C On CS.Id = C.Id 
INNER JOIN ContentType CT On C.ContentTypeId = CT.Id; 

則:

SELECT * FROM content_view WHERE Status = 5; 
相關問題