2014-10-09 60 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没有图像附件,这可能会或可能不符合您的需要。如果您提供样本数据或有关您的数据的更多详细信息,您可能会得到更完整的答案