mysql:从2个不同的表中选择所有行

时间:2018-10-25 15:14:14

标签: mysql

我在mysql dabase中有2个表,它们具有不同的行号和不同的列,所以我想一起显示所有结果

table_one:

id, name, surname, phone, email

table_two:

id,name,city,phone,website

所以table_one有30行,而table_two有10行 我怎么能一起展示

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

尝试了这个但没用

SELECT * FROM table_one UNION ALL SELECT * FROM table_two ;

2 个答案:

答案 0 :(得分:1)

我认为您只需要Left join。之所以使用左联接,是因为table_two中的所有id似乎都没有匹配的行:

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

答案 1 :(得分:0)

如果您希望将它们放在单独的行上,那么应该可以使用:

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