A
A
Alexey Mikhailov2021-06-13 21:57:21
MySQL
Alexey Mikhailov, 2021-06-13 21:57:21

How to make a condition for joining tables on several fields in LEFT JOIN?

There are two tables. In the `names` table, the fields are: `id`, `name`, `street`, `house`. In the table `professions` the fields are: `id`, `profession`, `street`, `house`. That is, in fact, the addresses are divided into two fields: street and house.

The question is how to join these LEFT JOIN tables with the condition that streets and houses coincide at the same time? I do like this, but it returns NULL instead of values ​​from the second table:

SELECT `names`.`name`, `professions`.`profession`
FROM `names`
LEFT JOIN `professions`
ON `names`.`street` = `professions`.`street` AND `names`.`house` = `professions`.`house`

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
d-stream, 2021-06-13
@DPhil

Actually syntactically everything is correct. The question is in the real match of rows in tables (spaces, encodings, etc.)
What will join only with the first and only the second condition give?

R
rPman, 2021-06-13
@rPman

Zaros looks correct, so something is wrong with the data.
Look at what the query displays if you leave only one condition, for example, on street? and display in select *

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question