0
Hello!
I have the following appointments:
SELECT id,
nome,
idade,
data_nascimento,
cpf
FROM internos
WHERE cpf = '12345678911'
and
SELECT id,
nome,
idade,
data_nascimento,
cpf
FROM externos
WHERE cpf = '12345678911'
I would like to know how to switch between the results:
How so?
If the date of birth of the result of the above query were null, bring the date of the below, the whole line:
I tried to do so:
SELECT id,
nome,
idade,
IFNULL(data_nascimento, A.data_nascimento),
cpf
FROM internos
WHERE cpf = '12345678911'
UNION
(
SELECT id,
nome,
idade,
data_nascimento,
cpf
FROM externos
WHERE cpf = '12345678911'
) A
Because sometimes there is no data in one table but there is in another
At first the result comes like this:
____________________________________________________________
[ id | nome | idade | data_nascimento | cpf ]
[ 1 | Carlos | NULL | NULL | 12345678911 ]
[ 15 | carlos | 20 | 1999-04-01 | 12345678911 ]
--------------------------------------------------------------
In consultation I would like the consultation to prevail from below
____________________________________________________________
[ id | nome | idade | data_nascimento | cpf ]
[ 15 | carlos | 20 | 1999-04-01 | 12345678911 ]
--------------------------------------------------------------
[EDIT]
If you have the complete data in the first result, the second is empty
If it is not null in the 2 tables what you want to do ?
– Alisson Marqui
In fact, if you complete the first one, you don’t have the second
– adventistaam
Why do it in SQL?!
– epx