简体   繁体   中英

SQL Join Multiple table query

I wrote following code:

 cmd = new SqlCommand("Select City.City, Company.Company,Emp_Depart.Department,Emp_Name.Uname from Emp_Name INNER join Emp_Name on" +
            "Emp_Name.Id=Emp_Depart.Id Emp_Name.Id=Company.Id Emp_Name.Id=City.Id where Emp_Name.Id=" + txtId.Text);
 cmd.Connection = con;
 da = new SqlDataAdapter(cmd);
 dt = new DataTable();
 da.Fill(dt);
 txtName .Text = dt.Rows[0][1].ToString().Trim();

I am getting following error

Additional information: Incorrect syntax near '.'.

I am newbie for Joins Please help me out...

You missed And operator in join condition

SELECT CITY.CITY,
       COMPANY.COMPANY,
       EMP_DEPART.DEPARTMENT,
       EMP_NAME.UNAME
FROM   EMP_NAME
       INNER JOIN EMP_DEPART
               ON EMP_NAME.ID = EMP_DEPART.ID
       INNER JOIN COMPANY
         ON EMP_NAME.ID = COMPANY.ID
       INNER JOIN CITY
         ON EMP_NAME.ID = CITY.ID 
WHERE EMP_NAME.ID =+ TXTID.TEXT

You need to add AND between multiple join conditions. Also, add a space after EMP_name on .

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