加入表来获取所需的数据,我有三个连接的表,我想从第三个表中获取数据

时间:2014-04-26 07:33:43

标签: sql database sql-server-2008 left-join inner-join

我的第一个表名是UserClient,它包含UserTrainerID。使用UserTrainerID,第二个表格为UserTrainer,其中包含UserID(我希望使用UserID了解UserTrainer的{​​{1}},现在是第三个表格为UserTrainerID,在User的帮助下,我想获得userID

2 个答案:

答案 0 :(得分:0)

Select a.username from t3 a,t2 b,t1 c 
    where a.userid=b.userid and b.UserTrainerID= c.UserTrainerID ;

答案 1 :(得分:0)

这是连接三个表的语法,我希望你能够相应地加入你的表:

SELECT t1.col, t3.col FROM table1 
             join table2 ON table1.primarykey = table2.foreignkey
             join table3 ON table2.primarykey = table3.foreignkey

了解详情:http://javarevisited.blogspot.com/2012/11/how-to-join-three-tables-in-sql-query-mysql-sqlserver.html#ixzz2zyYzv3pD

SQL Inner-join with 3 tables?