您好,我想执行更多 SQL 语句并得到一个结果,比如我有 3 个表,它们都有相同的列名:
Table user1
User Password Email
Table user2
User Password Email
Table user3
User Password Email
现在我想在一个 SQL 语句中从所有表中获取所有用户密码电子邮件。我希望你能帮助我。
感谢您的帮助。
使用UNION
.
SELECT User, PassWord, Email FROM Table1
UNION
SELECT User, PassWord, Email FROM Table2
UNION
SELECT User, PassWord, Email FROM Table3
上述查询将生成唯一记录。如果要保留重复,请添加ALL
SELECT User, PassWord, Email FROM Table1
UNION ALL
SELECT User, PassWord, Email FROM Table2
UNION ALL
SELECT User, PassWord, Email FROM Table3
Aunion
将允许您合并记录集。
select user, password, email from user1
union
select user, password, email from user2
union
select user, password, email from user3
很简单,使用 Union 运算符如下:
SELECT User, PassWord, Email FROM User1
UNION
SELECT User, PassWord, Email FROM User2
UNION
SELECT User, PassWord, Email FROM User3
select User, Password, Email
from Table1
union all
select User, Password, Email
from Table2
union all
select User, Password, Email
from Table3
联合操作是您问题的一个很好的例子:
select user, password, email from user1
union
selection user, password, email from user2
union
select user, password, email from user3