Site icon Komrod | Health Matters

LEFT JOIN crossing with multiple keys

Is there anyway to cross tables with 2 keys, in case the first one doesn’t exist?

For example:

I have a LEFT table called order that contains a field called key_1 and a RIGHT table called pre_order where key_1 is the primary key. The thing is: due to systemic errors, not all registers from order contain a key_1 value, even though they should. So, when left joining those tables, some data from my pre_order table will be missing. If key_1 is empty in the order table, I’d like it to cross with a different key. In this case, a CONCAT of multiple fields that are common to both tables, that would garantee an artificial primary key.

I thought about using COALESCE, but in a first glance, it wouldn’t work.

The logic is:

IF key_1 is not null nor empty, cross both tables with key_1

ELSE, cross it with a CONCAT of multiple common fields of both tables.

Unfortunately, it looks like the ON statement doesn’t let me work freely around logic statements.

Exit mobile version