Skip to content
Advertisement

JOIN ON either of the two columns but not both

I have two tables (orders, agents) i’m trying to join on either of the two columns but not both. There are some records in orders that have both of these columns populated and it returns as duplicate

orders:

agents:

I tried joining with an OR clause

I’m getting the following results

Expected Results

It looks in the case where both the agent_id and username are a match, its matching both and duplicating it my results. Is there a way to prevent the username match when the agent_id match is present.

Advertisement

Answer

You can left join twice, with the condition that evicts the second join if the first one matches:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement