Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I have two tables as shown below,

Table1:
SQL
---------------------------------------
SNO| Name  | ID| 
----------------------------------
1  | Aj Ali  | A001 |
2  | DB Kola | A002 |  
3  | Raj Sir | A003 |
----------------------------------


Table2 :
SQL
----------------------------------
SNO| Name  | 
----------------------------------
10  | Ali Aj  | 
20  | DB Kola Mj|   
30  | Sir Raj | 
----------------------------------


Now i need output like this,

SQL
----------------------------------
SNO| Name  | ID |
----------------------------------
10  | Ali Aj  |  A001 |
20  | DB Kola |  A002 |     
30  | Sir Raj |  A003 |  
----------------------------------


Here the problem is the "name" column values are not in the same order (First Name and SurName).

Please help me on this.

What I have tried:

SQL
select Table2.SNO,Table2.Name,Table1.ID from Table1 inner join Tabl2 on Table1.Name=Table2.Name


--------------------
And my output is only this,
SQL
----------------------------------
SNO| Name  | ID |
----------------------------------
20  | DB Kola |  A002 |     
----------------------------------
Posted
Updated 9-Feb-17 6:53am
v2

You need to re-design your tables to split the name into the relevant parts, so have a field for first name, surname, title etc. Secondly joining on text fields like name is very prone to error, so instead of storing the name in Table2 you should store the ID of table 1

SQL
----------------------------------
SNO| SNO_ID  | 
----------------------------------
10  | 1  | 
20  | 2 |   
30  | 3 | 
----------------------------------


That will let you do a query to get you the data you need

SQL
SELECT t2.SNO, t1.Name, T1.ID from Table1 t1 join Table1 t2 on t2.SNO_ID = t1.SNO
 
Share this answer
 
v2
As it is, you cannot be certain of the correct linkage between these two table though it seems to suggest the name fields in the respective tables. But the name values are not identical. That is the result of poor database design. Name cannot be unique, just imagine how many people can have the same names and the different ways of writing a name. Rightfully, they should be linked by the ID fields like this:
table 1        table 2
SNO            SNO
ID  ---------- ID
Name
This is just one example, the actual table design will depend on the requirements. Start learning Introduction to database design[^] and 1NF, 2NF, 3NF and BCNF in Database Normalization | DBMS Tutorial | Studytonight[^] and re-design your database.
 
Share this answer
 
Comments
Karthik_Mahalingam 9-Feb-17 23:11pm    
5
There is no automatic way to ensure that DB Kola is DB Kola Mj or not, just like 2 people can have the same name.
That is why no one use a name as a key between tables, never.
As a database designer, it is your job to ensure that such situation never happen.
 
Share this answer
 
Comments
Karthik_Mahalingam 9-Feb-17 23:11pm    
5
Patrice T 10-Feb-17 3:08am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900