我有两个表,表 1 和表 2。
表 1 的字段是:
book,pen,pencil,bag
表 2 的字段是:
car,van,book,bike,pencil
当我运行查询时,我希望查询忽略重复或公共字段并返回另一个字段。
输出应如下所示,
car,van,bike,pen,bag
我有两个表,表 1 和表 2。
表 1 的字段是:
book,pen,pencil,bag
表 2 的字段是:
car,van,book,bike,pencil
当我运行查询时,我希望查询忽略重复或公共字段并返回另一个字段。
输出应如下所示,
car,van,bike,pen,bag
也许:
SELECT x.thing FROM
(
SELECT thing FROM dbo.Table1
UNION ALL
SELECT thing FROM dbo.Table2
) X
GROUP BY x.thing
Having Count(*) = 1
但是,这也将删除其表中可能需要或可能不需要的重复项。
你有没有尝试过这样的事情:
delete form X
where (car =
Select distinct car
from X
where x);
distinct--> 返回不同的值。
试试这个:
declare @table1 table (col1 varchar(max))
declare @table2 table (col1 varchar(max))
insert into @table1 values
('book'),('pen'),('pencil'),('bag')
insert into @table2 values ('car'),('van'),('book'),('bike'),('pencil')
;with cte
as (
select COUNT(1) as total_item, col1 from (
select col1 from @table1
union all
select col1 from @table2
)a group by col1
)
select col1 from cte where total_item = 1
WITH uniontables AS (
SELECT NULL AS car,
NULL AS van,
book,
NULL AS bike,
pen,
pencil,
bag
FROM [Table 1 ]
UNION
SELECT car,
van,
book,
bike,
NULL AS pen,
pencil,
NULL AS bag
FROM [Table 2 ] )
SELECT DISTINCT * FROM uniontables