SQL SubQuery

2009/06/10

這篇不是教學文,是因為我太少寫SQL and T-SQL

常常忘記sub Query的下法特地上來memo一下

不要笑我,我不是學SQL出身的只是略懂..^^



SELECT * FROM CAM_CATALOG_CATEGORY as T1
WHERE T1.CATEGORY_ID IN
(
SELECT CATEGORY_ID FROM CAM_CATALOG_CATEGORY_RELATION as T2
WHERE T2.CATEGORY_ID=T1.CATEGORY_ID
) AND IMAGE_ID <> ''





通常這樣做效能都不是很好因為動用兩次Query,切記切記

再加上可以過濾另外一張table 的參考



SELECT * FROM CAM_CATALOG_CATEGORY as T1
WHERE (T1.CATEGORY_ID IN ( SELECT CATEGORY_ID FROM CAM_CATALOG_CATEGORY_RELATION as T2 WHERE T2.CATEGORY_ID=T1.CATEGORY_ID ) and T1.CATEGORY_ID not IN (SELECT LINK_ID FROM EMAP_MAPLINK as T3 WHERE T3.MAP_ID=20 ))AND IMAGE_ID <> '' AND CATEGORY_ID <> 20
SELECT * FROM EMAP_MAPLINK as T3 WHERE T3.MAP_ID=20


0 意見:

程式 . 生活 . D小調.@2010 | Binary Design: One Winged Angel.