You must Sign In to post a response.
  • Category: SQL Server

    How To Join Four(4) Tables in Sql Server

    Hi Developers,

    below is my code for joining 3 sql tables.

    select * from tblCostSheet_WithoutComponent cs inner join tbl_fr fr on cs.enquiryid=fr.enquiryid inner join tbl_enquiryregister er on fr.enquiryid=er.enquiryid where cs.status='A' and cs.Approval_status='Approved' and costsheet_status='Pending Quotation' order by cs.costsheet_id asc

    I want to join 4 sql tables.instead of my code.
    So how to add one more table friends.give me some solution.

    Thanking you,
    Paul.S
  • #765695
    Hi

    When you post query issue before your post table structure script should be need then only reply members easy find your issue.Keep them follow

    Try this Query sample

    --SELECT * FROM TABLE1 T1 INNER JOIN T2 on T1.ID=T2.ID
    --INNER JOIN T3 ON T3.ID=T2 INNER JOIN T4 ON T4.ID=T3.ID
    --HERE IMPLEMENTED YOUR WHERE CONDITION

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765698
    Hi,
    Refer this:
    4 tables with name
    1.Table1
    2.Table2
    3.Table 3
    4.Table 4
    Select A.Column1, B.Column1, C.Column1, D.Column1
    from Table1 A
    inner join Table2 B on A.PK = B.FK
    inner join Table3 C on A.PK = C.FK
    inner join Table4 D on A.PK=D.FK;

  • #765700
    You can join the table as much as you can based on your requirement.

    select * from tblCostSheet_WithoutComponent cs
    inner join tbl_fr fr on cs.enquiryid=fr.enquiryid
    inner join tbl_enquiryregister er on fr.enquiryid=er.enquiryid
    inner join tbl4 T4 on T4.condition=joincondition
    where cs.status='A' and cs.Approval_status='Approved' and costsheet_status='Pending Quotation' order by cs.costsheet_id asc

    Just add 4th table in the inner join.

    By Nathan
    Direction is important than speed

  • #765703
    Hi Paul,

    Joining 4 tables is not a matter, before join the tables there should be relation between the tables, if there is a relation then based on condition you have to join how you join for 3 tables.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765742
    Thanks for all of your useful replies Friends.
    Today only i got exact solution for that problem.
    Thank you very much, god may bless you.

    by
    Paul.S

  • #765882
    select Columname1.Tablename1,ColumName2.TableName2,ColumnName3.TableName3,ColumnName4.TableName4

    from TableName1

    inner join TableName2 on TableName1.ColumnName=TableName2.ColumnName

    inner Join TableName3

    on TableName3.ColumnName=TableName2.ColumnName

    inner join TableName4 on TableName4.ColumnName=TableName3.ColumnName where TableName3.Columname='EmpName'

  • #766518
    Hi,
    You can add more tables in Join query if you need. But, you have to add some what reasonable column have to add in the query. because, while using join, it must satisfy all the conditions then only it will be give results.

    Kindly use the following Query. It may help you,

    select * from tblCostSheet_WithoutComponent cs
    inner join tbl_fr fr on cs.enquiryid=fr.enquiryid
    inner join tbl_enquiryregister er on fr.enquiryid=er.enquiryid
    inner join tbl4 T4 on T4.condition=joincondition
    where cs.status='A' and cs.Approval_status='Approved' and costsheet_status='Pending Quotation' order by cs.costsheet_id asc

    Regards,
    Karunanidhi.K


Sign In to post your comments