Gloytos htyqo
Gloytos htyqo

Reputation: 355

mysql: select all rows from 2 different tables

i have 2 tables in mysql dabase with different row numbers and different column, so i want to display all results together

table_one:

id, name, surname, phone, email

table_two:

id,name,city,phone,website

so table_one has 30 rows and table_two has 10 rows how can i show all together

id,name,surname,city,phone,email,website

Tried this but is not working

SELECT * FROM table_one UNION ALL SELECT * FROM table_two ;

Upvotes: 0

Views: 285

Answers (2)

isaace
isaace

Reputation: 3429

If you want to have them on separate rows then this should work:

select id,name, '' as surname, city,phone, '' as email, website from table1
union all
select id,name,surname,city,phone,email,website from table2

Upvotes: 0

Madhur Bhaiya
Madhur Bhaiya

Reputation: 28864

I think you simply need Left join. Left join is used because it seems that you don't have matching rows for all the id(s) in the table_two:

SELECT t1.id,
       t1.name, 
       t1.surname, 
       t2.city, 
       t1.phone, 
       t1.email, 
       t2.website 
FROM table_one AS t1
JOIN table_two AS t2 ON t2.id = t1.id

Upvotes: 1

Related Questions