Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So there are 2 tables in my database.

staff:
name      age    mail
John      20     john@john.john
Robert    25     robert@robert.robert


customers:
name     age    mail
Bob      21     bob@bob.bob
Mara     20     mara@mara.mara
Trisha   20     trisha@trisha.trisha
Melina   23     melina@melina.melina

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', 'mara@mara.mara')
('Trisha, '20', 'trisha@trisha.trisha')

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' 'john@john.john')
('Mara', '20', 'mara@mara.mara')
('Trisha, '20', 'trisha@trisha.trisha')
question from:https://stackoverflow.com/questions/65907119/query-2-similar-tables-sqlite

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
92 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...