简体   繁体   English

如何从多个表中选择相同的列

[英]How to select the same column from multiple tables

I have 2 tables as follows, with the same columns 我有两个表如下,具有相同的列

Table 1
Name             Subject
Alan Smith       Biology
Fred Jones       Biology
Anne Pool        Chemistry


Table 2
Name             Subject
Paul Jones       Chemistry 
Ted Williams     Biology

Question: What SELECT statement will retrieve just rows where Subject is Biology from the 2 tables? 问题:什么SELECT语句将从2个表中检索主题生物学的行?

SELECT Name 
FROM table2 
WHERE Subject='Biology' 
    UNION SELECT Name 
    FROM table1 
    WHERE Subject='Biology'

UNION combines the result sets from multiple SELECT statements, while using it, the columns in both tables have to be the same. UNION组合了多个SELECT语句的结果集,在使用它时,两个表中的列必须相同。

在每个表上执行两个SELECT * FROM myTable WHERE subject = 'biology'查询并处理该数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM