Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How conditional join if first matching has no row, use second matching

My client table has some special rows that work this way. There are multiple customer records that belongs to the same customer but with slightly different column value. The tables belong to them and I can't change anything in their table, so I have to work on a script to deal with this.

Now I need to compare customer data from main table and get the data from client table. The condition is as below:

  1. If the main table Id exists in client table, get the only record matching with main table Id column

  2. If the main table Id not exists in client table, use main table Idnumber to find and match Idnumber in client table.

Below is an example of what I'm trying to achieve:

Let's say this data exist in the main table and client table below:

enter image description here

In the scenario above, my script should always pick the client table PKId 1 only and ignore the row PKId 2 in client table by matching the main table Id column and client table ClientId column.

And for another scenario below:

enter image description here

Since the row PKId 1 has empty ClientId and there's no way to match the client Id 10 in client table, my script should use main table Idnumber to find and match record in the client table and would pick up the row PKId 2 by the Idnumber column.

I wanted to do a case in the join condition but not sure how I should construct it. I'm thinking about something like below (not actual SQL statement, just some idea):

Select 
    c.Id, c.Name, c.AuthorizeToken 
From 
    Client c 
Left Join 
    Main m on (If m.Id = c.ClientId has data return, get AuthorizeToken from that row only;
 else if m.Id = c.ClientId has no data return, use m.Idnumber = c.Idnumber     
 to find and get AuthorizeToken) 

Appreciate if can advice me on any alternative to achieve this.

like image 779
Koo SengSeng Avatar asked Sep 02 '16 05:09

Koo SengSeng


People also ask

Which join is used when there is no match?

A LEFT JOIN will always include the rows from the LEFT table, even if there are no matching rows in the table it is JOINed with. When there is no match, the corresponding rows will use NULL to represent the missing values from the second table.

How would you return data from 2 tables even if there are no matches?

The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.

Which join clause is used to return the rows that do not have the matching value?

LEFT JOIN is used; this will return ALL rows from Table1 , regardless of whether or not there is a matching row in Table2 .

Can we apply joins on 2 tables which has no related data?

Yes, you can! The longer answer is yes, there are a few ways to combine two tables without a common column, including CROSS JOIN (Cartesian product) and UNION. The latter is technically not a join but can be handy for merging tables in SQL. In this article, I'll guide you through the different solutions with examples.


1 Answers

You can also use LEFT JOIN instead

SELECT m.id, ISNULL(c.authorize_token, c1.authorize_token) authorize_token
FROM main m
LEFT JOIN client c ON m.id = c.client_id
LEFT JOIN client c1 ON m.idnumber = c1.idnumber
like image 69
Susang Avatar answered Oct 13 '22 00:10

Susang