在2个或更多表上使用SELECT

时间:2015-09-12 13:11:14

标签: php mysql sql select join

考虑以下数据库:

浏览器表:

id | name        | description                     | stuff different from cars table
-------------------------------------------------------------------------------------
 1 | Chrome      | Some description
 2 | Firefox     | Some other description
 3 | Vivaldi     | Even more description

汽车表:

id | name        | description                      | stuff different from browsers table
-------------------------------------------------------------------------------------
 1 | Hyundai     | Some korean description
 2 | Ford        | Some ford ther description
 3 | Ferrari     | Even ferrari more description

我需要在PHP中获得的输出是6个具有id,名称和描述的对象。我可以使用join关键字执行此操作吗?如果是这样的话......我怎么样,悄悄地研究几个小时。或者可能采用不同的方法?

如果我要创建一个我需要获得的输出数据表,那就是:

id | name        | description                   
------------------------------------------------
 1 | Hyundai     | Some korean description
 2 | Ford        | Some ford ther description
 3 | Ferrari     | Even ferrari more description
 1 | Chrome      | Some description
 2 | Firefox     | Some other description
 3 | Vivaldi     | Even more description

1 个答案:

答案 0 :(得分:5)

这不是join的用例。由于您希望两个表中的行一个接一个,而不是并排,您应该使用union all

SELECT id, name, description
FROM   browsers
UNION ALL
SELECT id, name, description
FROM   cars