如何显示教员姓名及其名称

时间:2017-05-23 14:38:53

标签: mysql

如何使用sql中的join函数显示教员名称及其hod名称?

F.id    f.name  G    DOB          DOJ        MOB.NO     Depid   HOD 

F001    Ramesh  M   1983-01-21  2015-01-16  998377639   D001    
F002    Anu F       1972-10-18  2015-08-18  945763653   D020    
F003    Kannan  M   1979-02-20  2016-02-20  594764848   D013    
F004    Naveen  M   1983-02-21  2016-02-21  985473484   D001    F001
F005    Vijay   M   1983-04-02  2016-04-23  598374394   D020    F002
F006    Tara    F   1984-04-06  2015-03-04  801234722   D001    F001
F007    Akila   F   1974-12-13  2015-10-12  944441234   D010    
F008    Raju    M   1978-06-08  2016-08-12  923881299   D013    F003
F009    Uma     F   1973-07-12  2015-03-09   598237632  D010    F007
F010    Vimala  F   1980-05-27  2015-03-17  984210732   D013    F003

1 个答案:

答案 0 :(得分:0)

试用SELF JOIN

SELECT f.name, b.HOD
FROM table_name f, table_name b
WHERE f.id <> b.HOD
AND f.id = b.id
ORDER BY b.HOD;

或使用嵌套查询

SELECT f.name, HOD
FROM table_name
WHERE f.id  = (SELECT HOD FROM table_name)
ORDER BY HOD;