Razvan M.
Razvan M.

Reputation: 407

Using SELECT on 2 or more tables

Consider the following database:

Browsers table:

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

Cars table:

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

The output that I need to get in PHP is 6 objects with an id, a name and a description. Can I do that with a join keyword? If so... How, I've been quietly researching for hours. Or maybe a different approach?

If I were to make a table of the output data I need to get, that would be:

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

Upvotes: 4

Views: 75

Answers (1)

Mureinik
Mureinik

Reputation: 311228

This isn't a usecase for a join. Since you want to have rows from both tables one after the other, not side by side, you should use a union all:

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

Upvotes: 5

Related Questions