S
S
seredaes2015-12-16 22:55:06
MySQL
seredaes, 2015-12-16 22:55:06

How to select from one table with a condition from the second?

The question is primitive, but I'm stuck with it :(
First table with users:
| id | name |
| 1 | User1 |
| 2 | User2 |
Second table with user's cars:
| id | user_id | car |
| 1 | 1 | AUDI |
| 2 | 1 | AUDI |
| 3 | 2 | AUDI |
If the car is not specified, select only users.
If it is specified, for example, AUDI, then displays 3 times the users:
twice the first user and 1 time the second. The query is as follows:
SELECT * FROM tab1 LEFT JOIN tab2 ON tab1.id=tab2.user_id
And everything seems to be correct, but how can I display the user only once?
I see the solution in adding instead of * - DISTINCT tab1.id
But maybe there is a more beautiful solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IceJOKER, 2015-12-16
@seredaes

GROUP BY users.id?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question