Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join two tables based on substring values of fields?

I am having problem with sql. I want to join two tables, employee and class instructor. Condition is that employee is having unid column like 'u0871457' where as class instructor is having EmplId as '00871457'.

I just want to replace the first character of EmplId to 'u' to join to match the string coming from unid . How can I do that? I have tried this so far :

select e.name, i.name
from  Employee e
inner join Instructor i on SUBSTR(e.id,1, LENGTH(e.id )) = SUBSTR(i.id,1, LENGTH(i.id ))

but this is resulting into a blank resultset.

Any help will be appreciated. Thanks for your time!

like image 408
userx Avatar asked Feb 20 '15 18:02

userx


People also ask

How do you join two tables based on conditions?

You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least one column from another. The join creates a temporary composite table where each pair of rows (one from each table) that satisfies the join condition is linked to form a single row.

What is the most efficient way of joining 2 table in same database?

Using the JOIN operator is the most common method for joining multiple tables in a database. In the article "An Illustrated Guide to the SQL INNER JOIN", I discuss more about this operator. The article "SQL INNER JOIN Explained in Simple Words" also delves into this topic further.

Can we use substring in Group by clause?

Yes I agree with you. But if substr(country,0,20) is same for 2 country , but the actual value of country is different, in that case, we can't display both country value in a group by output.

Which join extracts data of matching rows from both tables?

INNER JOIN This type of join returns those records which have matching values in both tables. So, if you perform an INNER join operation between the Employee table and the Projects table, all the tuples which have matching values in both the tables will be given as output.


1 Answers

So many ways to do this. It would be a good idea to look at the explain plan for various ways before committing to a particular method. For example, if there is a function-based index on EMPLOYEE such as SUBSTR(id, 2, LENGTH(id) - 1) then you'll want to use that in your query:

SELECT e.name, i.name
  FROM employee e INNER JOIN instructor i
    ON SUBSTR(e.id, 2, LENGTH(e.id) - 1) = SUBSTR(i.id, 2, LENGTH(i.id) - 1);

Another question is if the values in the id column are always the same length in EMPLOYEE and INSTRUCTOR. What if they are of differing lengths? Maybe one has more padding than another. Also, will they always be digits apart from a leading u? If so, then it might be worthwhile to try a safe TO_NUMBER() conversion:

SELECT e.name, i.name
  FROM employee e INNER JOIN instructor i
    ON TO_NUMBER(REGEXP_SUBSTR(e.id, '\d+$')) = TO_NUMBER(REGEXP_SUBSTR(i.id, '\d+$'));

One other thing you may want to consider, however -- is there a reason for the leading u in the EMPLOYEE id column? Can there be other leading characters? Does the leading u stand for something (violating first normal form, but that happens)?

like image 137
David Faber Avatar answered Oct 12 '22 07:10

David Faber