2014-10-09 46 views
0

我正在爲這個查詢而努力。試圖選擇沒有附加/分配圖像的項目。選擇記錄到沒有附加圖像的地方

select distinct a.itm_id, a.itm_num, d.att_type from saitem as a 
    join saassignment as b on b.itm_id = a.itm_id 
    join sacatalogitem as c on c.itm_id = a.itm_id 
    join saattachment as d on d.att_id = b.att_id 
     where c.cat_id = '307' 
       and d.att_type not like '%application%' 
       and d.att_type not like '%text%' 
       and d.att_type not like '%url%' 
       and d.att_type not like '%image%' 
         order by a.itm_id ASC 

我濾除了所有其他附件類型(應用程序,文本和網址)。如果在saitem表中沒有包含附件類型%image%,如何告訴sql選擇該記錄?現在我正在過濾掉所有%image%附件類型,因爲我不知道如何正確定義以獲取我正在尋找的內容...

編輯:我試圖選擇表中的所有記錄SAITEM,其中表SAATTACHMENT中沒有鏈接的「%image%」附件類型(列爲att_type)。我的問題是att_type列包含不同的類型,如應用程序,文本和URL,所以我不能只寫沒有圖像附件的地方。

樣本數據:

enter image description here

+1

這完全不清楚您要問什麼。你能澄清你的問題嗎? – 2014-10-09 17:55:01

+0

Porvide一些示例數據... – 2014-10-09 17:57:55

+0

我添加了來自每個表的示例數據的圖片 – 2014-10-09 18:35:16

回答

1

有幾種方法可以做到這一點。

一種方法是做一個反連接

select distinct a.itm_id, a.itm_num, d.att_type from saitem as a 
     join saassignment as b on b.itm_id = a.itm_id 
     join sacatalogitem as c on c.itm_id = a.itm_id 
     LEFT join saattachment as d on d.att_id = b.att_id 
       and d.att_type like '%image%' 
    where c.cat_id = '307' 
    and d.att_id is null 

這需要你的INNER JOIN,並使其成爲一個LEFT JOIN。然後它在RIGHT側的連接區域上測試NULL NULL

此外在技術上只發現任何saassignment沒有圖像附件,這可能會或可能不符合您的需要。如果您提供樣本數據或有關您的數據的更多詳細信息,您可能會得到更完整的答案

相關問題