1
I have a chart that can record the id
reference (FK) in more than one column.
I need to make a select that links to the first reference column and if it is null, make the link through the second column.
My query:
SELECT
*
FROM nfs n
, nfs_item ni
, pedido_entrega pe
WHERE n.id = ni.nfs_id
AND ni.pedido_entrega_id = pe.id
See that the nfs_item
relates to pedido_entrega
through the pedido_entrega_id
. However, sometimes this column will be of value null
and the reference will be recorded in a column called pedido_entrega_id_origem
.
Therefore, time I will relate these tables through the ni.pedido_entrega_id = pe.id
and time I will relate them through the ni.pedido_entrega_id_origem = pe.id
. How do I do that?
Is there any case where Table 1 has the two columns filled (id_table1 and id_source ) ?
– Caique Romero
No, she’ll either have the info on one or the other, never both together.
– Régis Neis
All right, I’ll set it right here and let you know, thanks in advance.
– Régis Neis
Hey, good morning, it didn’t work out. I’m going to put the query here, I think it helps understanding: select * from nfs n, nfs_item ni, pedido_delivery pe Where n.id = ni.nfs_id and ni.pedido_entrega_id = pe.id See that nfs_item relates to the pedi_delivery through the pedi_entrega_id. However, sometimes this column is empty and this id is saved in a column called pedi_entrega_id_origin. Therefore, time I will relate these tables through the "ni.pedido_entrega_id = pe.id" and time I will relate them through the "ni.pedido_entrega_id_origin = pe.id". How do I do that?
– Régis Neis
I changed my answer, I also did a simulation online and it worked. See if it fits you.
– Caique Romero
Put the database q vc ta using and if possible show us the tables or at least their fields....
– Matheus Suffi
Use Oracle database, but managed to solve, I will complement with a reply. Thanks for the help.
– Régis Neis