I have a table that can write id
fence (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 nfs_item
is related to pedido_entrega
through pedido_entrega_id
. However, sometimes this column will be null
and the reference will be written to a column named pedido_entrega_id_origem
.
So I'll time these tables through ni.pedido_entrega_id = pe.id
and time I'll relate them through ni.pedido_entrega_id_origem = pe.id
. How do I do this?