CalinN96
CalinN96

Reputation: 15

Query 2 similar tables SQLITE

So there are 2 tables in my database.

staff:
name      age    mail
John      20     [email protected]
Robert    25     [email protected]


customers:
name     age    mail
Bob      21     [email protected]
Mara     20     [email protected]
Trisha   20     [email protected]
Melina   23     [email protected]

If I want to select everything from customers where age is 20 I use this:

extracting = mydb.execute("SELECT * FROM customers WHERE age = '20'")
20s_users = extracting.fetchall()

for i in 20s_users:
  print(i)

And the output in python is

('Mara', '20', '[email protected]')
('Trisha, '20', '[email protected]')

But I want to select this thing from BOTH tables and combine them...

What query command should I use in order for my output to be:

('John' '20' '[email protected]')
('Mara', '20', '[email protected]')
('Trisha, '20', '[email protected]')

Upvotes: 0

Views: 30

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65433

You just need a UNION ALL clause such as

extracting = mydb.execute("""
                           SELECT * 
                             FROM
                             (
                              SELECT * FROM customers
                              UNION ALL
                              SELECT * FROM staff
                             ) AS cs  
                            WHERE age = 20  
                          """)

where quotes wrapping up matching values for age column are removed considering the data type as numeric.

Upvotes: 1

Related Questions