简体   繁体   中英

Use different records from one table to combine it with the different entries of other

I have two tables. Employee and Qualifications.

Employee

Emp_id   Name   Qualification1   Qualification2    Qualification3
10001   xxxxxx         1              3                5
10002   yyyyyy         3              2                       
.......
......
.....

Qualifications

Qual_ID   Qual_name
    1      B.Tech
    2      MCA
    3      M.Tech

How can i use join query to get the following output

Emp_ID   Name    Qual1   Qual2   Qual3
10001   xxxxxxx  B.Tech   MCA     pppp
10002   yyyyyyy  B.Tech   
......
.....
.....

try this

Select E.Emp_ID,E.Name,Q1.Qual_Name,Q2.Qual_Name, Q3.Qual_Name
From 
    Employees AS E
    INNER JOIN Qualifications As Q1 ON E.Qualification1=Q1.Qual_ID
    INNER JOIN Qualifications As Q2 ON E.Qualification2=Q2.Qual_ID
    INNER JOIN Qualifications As Q3 ON E.Qualification3=Q3.Qual_ID
SELECT e.emp_id, 
       e.name, 
       q1.qual_name, 
       q2.qual_name, 
       q3.qual_name, 
FROM   employee e 
       INNER JOIN qualifications q1 
               ON e.qualification1 = q1.qual_id 
       INNER JOIN qualifications q2 
               ON e.qualification2 = q2.qual_id 
       INNER JOIN qualifications q3 
               ON e.qualification3 = q2.qual_id 

Look at the live working example here at this link:

SQLFiddle

This is working fine.

Try this:

Sql Fiddle

Select
    Emp_id, Name, 
    (Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification1) as Qual1,
    (Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification2) as Qual2,
    (Select Qual_name from Qualifications where Qualifications.Qual_ID = Employee.Qualification3) as Qual3
From Employee

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM