使用JOIN和UNION从两个表中选择元素,不包括列中的ID

时间:2015-09-29 13:22:20

标签: sql-server join union

我有3个这样的SQL表:

    tbl_items
id ... name ... value ... active
1      color    red       1
2      style    modern    1
3      age      old       1
4      size     small     1

    tbl_adv_items
id ... name ... value ... active
1      texture  suave     0
2      material plastic   1

    tbl_items_classes
id ... item_id
1      1
2      3

我想从条件tbl_itemstbl_adv_items中选择所有ID:如果tbl_items列中的item_id列中的tbl_items_classes列,则排除这些项目}

现在我有这个问题:

SELECT tbl_items.id FROM tbl_items 
JOIN tbl_items_classes ON tbl_items_classes.item_id <> tbl_items.id 
WHERE tbl_items.active = 1 
UNION ALL SELECT id FROM tbl_adv_items WHERE active = 1

它为我提供了所有项目,包括ID为item_id的项目。不应返回tbl_items中的ID 1 3 。我想我需要JOIN,但我无法使其正常工作

3 个答案:

答案 0 :(得分:2)

使用EXCEPT

SELECT id FROM tbl_items WHERE tbl_items.active = 1 

UNION ALL 

SELECT id FROM tbl_adv_items WHERE active = 1

EXCEPT 

SELECT item_id FROM tbl_items_classes

这样可以排除表item_id中包含的所有tbl_items_classes值。

Demo here

修改

如果您还想知道查询返回的id的来源(OP中未明确说明的内容),那么您可以使用以下查询:

(SELECT id,  0 AS origin_table FROM tbl_items WHERE tbl_items.active = 1 

UNION ALL 

SELECT id,  1 AS origin_table FROM tbl_adv_items WHERE active = 1)

EXCEPT 

(SELECT item_id, 0 AS origin_table FROM tbl_items_classes

UNION ALL

SELECT item_id, 1 AS origin_table FROM tbl_items_classes)

答案 1 :(得分:1)

出于性能原因,请使用经典LEFT JOIN

<强> DEMO

WITH cte AS (
  SELECT id , 'tbl_items' AS origin
  FROM tbl_items 
  WHERE active = 1 
  UNION ALL 
  SELECT id, 'tbl_adv_items'  AS origin
  FROM tbl_adv_items 
  WHERE active = 1
)
SELECT c.id, c.origin
FROM cte c
LEFT JOIN tbl_items_classes cl
  ON c.id = cl.item_id
WHERE cl.item_id IS NULL; 

左二案。

enter image description here 资料来源:http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

答案 2 :(得分:0)

您可以简单地在表之间建立联接并添加条件来过滤记录。试试这个:

select * from tbl_items tblA
inner join tbl_adv_items tblB on tblA.id = tblB.Id
left outer join tbl_items_classes tblC on tblA.id = tblC.item_Id
where tblC.Id is null -- this check will exclude desired items