日期:2014-05-18  浏览次数:20379 次

三表连接查询(在线等)急求
t1   01,02,03
t2   01,11
t3   01,13


这样查为什么不对急求
SELECT   *
FROM   t1
left   join   t2   ON   t1.[01]   =   t2.[01]
left   join   t3   ON   t1.[01]   =   t3.[01]

------解决方案--------------------
不能用01,02,03作为列名,换成字母就可以了
比如:
SELECT *
FROM t1
left join t2 ON t1.a = t2.a
left join t3 ON t1.a = t3.a

------解决方案--------------------
t1.后面跟的应该是字段名..
------解决方案--------------------
--这样


create table t1([01] int, [02] int, [03] int)
insert t1 select 1,2,3
insert t1 select 2,3,4

create table t2([01] int, [11] int)
insert t2 select 1, 11

create table t3([01] int, [13] int)
insert t3 select 2, 21

select * from t1
left join t2 on t1.[01]=t2.[01]
left join t3 on t1.[01]=t3.[01]

--result
01 02 03 01 11 01 13
----------- ----------- ----------- ----------- ----------- ----------- -----------
1 2 3 1 11 NULL NULL
2 3 4 NULL NULL 2 21

(2 row(s) affected)