我如何从两个 SQL table 中搜索条目并知道它来自哪个 table?

How do I search for an entry out of two SQL tables and know which table it came from?

我正在尝试查找特定条目。此条目只能出现在我的两个 table 中的一个中,并且永远不会在 table.

中重复出现

这是我的 tables 的缩小版示例:

Table 1:

Date             Name              Room
2020/01/23       John              201
2020/01/22       Rebecca           203

Table 2(没有相同数量的列):

Date             Name              
2020/01/23       Robert            
2020/01/22       Sarah             

要查找此条目,我需要指定日期和名称。你可以假设名字永远不会重复。

假设我想找到 Sarah 2020/01/22

她可能会出现在 Table 1 或 Table 2 中,我不知道是哪一个,我需要知道她在哪个 table 中。

我不确定如何在单个 SQL 查询中执行此操作。到目前为止,我只有两个独立的:

SELECT date,name from Table1 WHERE name="Sarah" and date='2020/01/22'

SELECT date,name from Table2 WHERE name="Sarah" and date='2020/01/22'

有没有一种方法可以在单个查询中完成,同时告诉我它来自哪个 table?它可能是另一个字段或我可以获得的一些指示。谢谢。

使用 union all,并向每个结果集添加另一列,其文字值表示 table 名称:

select 't1' as which, date, name from table1 where name = 'Sarah' and date = '2020-01-22'
union all 
select 't2' as which, date, name from table2 where name = 'Sarah' and date = '2020-01-22'