anm*_*rti 5 sql t-sql sql-server
我UNION或多或少要这样做:
select [table_name], name, address
from Employees
where [my_condition]
UNION
select [table_name], name, address
from Employees_history
where [my_condition]
Run Code Online (Sandbox Code Playgroud)
检索到的数据将在Employees或Employees_history中,但不在两个表中.
我需要知道数据来自哪个表.
SELECT 'Employees' AS [table_name],
name,
address
FROM Employees
WHERE [my_condition]
UNION ALL
SELECT 'Employees_history' AS [table_name],
name,
address
FROM Employees_history
WHERE [my_condition]
Run Code Online (Sandbox Code Playgroud)
我使用UNION ALL而不是UNION因为两个分支中没有重复.因此,它可以避免一些不必要的工作,删除整个结果集中的重复项.
如果分支机构中可能存在重复项DISTINCT,则添加到个人SELECT(s)