sql - Joining different table on a condition -


i have 3 tables, student, staff, transport, in transport table there records of students , staff have transport , differentiated candidatetype column. if candidatetype = 0 record must come staff table , if candidatetype=1 record must come student table. problem joining column in both tables have same values. below structure of tables.

|staff table| ------------------------- id | name | designation | ------------------------- 1  | abc  | teacher     | 2  | xyz  | clerk       | 3  | ddd  | teacher     |  student table id | name | class | ------------------- 1 | ali   | 5th   | 2 | khan  | 6th   | 3 | jan   | 6th   |  transport table id | candidateid | candiadate type | vehicleid ---------------------------------------------- 1  |  1          | 0               |  1 2  |  3          | 1               |  1 3  |  2          | 1               |  1 

i want retrieve record of students , staff in vehicleid=1. in transport table candidateids same in both staff , student table, how record of students , staff in vehicle one. help.....

select  a.id,         case when candidatetype = 0              b.name             else c.name         end name    transport         left join staff b             on  a.candidateid = b.id         left join   student c             on a.candidateid = c.id   a.vehicleid = 1 

output

╔════╦══════╗ ║ id ║ name ║ ╠════╬══════╣ ║  1 ║ abc  ║ ║  2 ║ jan  ║ ║  3 ║ khan ║ ╚════╩══════╝ 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -