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

cbjzeqam  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(346)

我在mysql dabase中有两个不同行号和列的表,所以我想一起显示所有结果
表1:

id, name, surname, phone, email

表2:

id,name,city,phone,website

那么表1有30行,表2有10行,我怎么能一起显示呢

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

试过了,但不起作用

SELECT * FROM table_one UNION ALL SELECT * FROM table_two ;
eyh26e7m

eyh26e7m1#

如果您想将它们放在不同的行上,那么这应该可以:

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

4xrmg8kj2#

我想你只需要 Left join . 使用left join是因为似乎没有匹配的行来表示 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

相关问题