alexj
alexj

Reputation: 149

MySQL Executing more SQL Statements and getting one Result

Hello I would like to execute more SQL Statements and get one result like lets say I have 3 Tables they all have same Column Names:

Table user1
User Password Email

Table user2
User Password Email

Table user3
User Password Email

Now I want to get all User Password Emails from all the Tables in one SQL Statement. I hope you can help me.

Thank you for your help.

Upvotes: 0

Views: 73

Answers (5)

Gaurav
Gaurav

Reputation: 123

Union operation is very good example for your question:

select user, password, email from user1
union
selection user, password, email from user2
union
select user, password, email from user3

Upvotes: 0

Mudassir
Mudassir

Reputation: 187

Quite simple, use Union operator as below :

SELECT User, PassWord, Email FROM User1
UNION 
SELECT User, PassWord, Email FROM User2
UNION
SELECT User, PassWord, Email FROM User3

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94489

A union will allow you to combine the record sets.

select user, password, email from user1
union 
select user, password, email from user2
union
select user, password, email from user3

Upvotes: 1

Niels Keurentjes
Niels Keurentjes

Reputation: 41968

select User, Password, Email
from Table1
union all
select User, Password, Email
from Table2
union all
select User, Password, Email
from Table3

Upvotes: 0

John Woo
John Woo

Reputation: 263803

use UNION.

SELECT User, PassWord, Email FROM Table1
UNION 
SELECT User, PassWord, Email FROM Table2
UNION 
SELECT User, PassWord, Email FROM Table3

The above query will generate unique records. If you want to keep duplicate, add ALL

SELECT User, PassWord, Email FROM Table1
UNION ALL
SELECT User, PassWord, Email FROM Table2
UNION ALL
SELECT User, PassWord, Email FROM Table3

Upvotes: 1

Related Questions