S
S
SimBioT192020-02-06 18:36:36
MySQL
SimBioT19, 2020-02-06 18:36:36

How to select the required fields from the database depending on the result of a LEFT JOIN?

Conditional request example:

SELECT IFNULL(t1.first_name, t2.username) as first_name, 
       IFNULL(t1.last_name, t2.domain) as last_name
FROM user u
LEFT JOIN table1 t1 ON t1.user_id = u.id AND user.type = 1
LEFT JOIN table2 t2 ON t2.user_id = u.id AND user.type = 2


How would it be correct to compose such a query (without IFNULL), given that it could be table3, etc.?

MySQL 8

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2020-02-06
@BorisKorobkov

The request is correct, exactly like this (with IFNULL) and it should be.
And that it was not necessary so to be perverted - normalize tables.

L
Lazy @BojackHorseman MySQL, 2020-02-06
Tag

COALESCE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question