Joining 4 Tables in SQL Server Using Join

被刻印的时光 ゝ 提交于 2021-02-18 02:00:52

问题


Hi Friends I have to join 4 tables in SQL Server and need to Show Specific Coulmns in Data Grid View

First Table : emp_details

enter image description here

Second Table : tbl_designation

enter image description here

Third Table : tbl_empcontribution

enter image description here

Forth table : tbl_empdeduction

enter image description here

Columns need to show in Data Grid View is

1.From First Table i need to show emp_id, emp_name , emp_pf
2.from Second Table i need to show designation_name
3.from third Table i need to show pfacc1 and pfacc2
4.From Fourth Table i need to show pf_percent and pf_max

Try to Reply to this as soon as possible Friends...Thanks..


回答1:


Below is the query you need, you should consider keeping your naming conventions consistent because this helps visually and reduce bugs when writing code.

SELECT ed.emp_id, ed.emp_name , ed.emp_pf, emd.designation_name, te.pfacc1, te.pfacc2, temp. pf_percent, temp.pf_max
FROM dbo.emp_details AS ed
LEFT JOIN dbo.emp_designation AS emd ON emd.designation_id = ed.emp_designation 
LEFT JOIN dbo.tbl_empcontribution AS te ON te.eid = ed.emp_id
LEFT JOIN dbo.tbl_empdeduction AS temp ON temp.eid = ed.emp_id



回答2:


    SELECT e1.emp_id, e1.emp_name, e1.emp_pf, e2. designation_name, e3.pfacc1, e3.pfacc2, e4. pf_percent,  e4.pf_max 

from emp_details e1, tbl_designation e2, tbl_empcontribution e3, tbl_empdeduction e4

 where e1.emp_id= e2.emp_id and e2.emp_id=e3.emp_id and e3.emp_id= e4.emp_id;

assuming you have emp_id as foreign key in all 4 tables



来源:https://stackoverflow.com/questions/17440556/joining-4-tables-in-sql-server-using-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!