这个有点奇怪
有人写了一个我认为不起作用的sql,但它确实可以,而且还返回了正确的结果。我已经写了一个简化的示例,但我认为它说明了这一点。
drop table #client; drop table #transactions; drop table #history; create table #client ( clientId int, name varchar(50) ); create table #transactions ( transid int, clientId int, Amount int ); create table #history ( transid int, Amount int ); insert into #client values (1, 'User 1'); insert into #client values (2, 'User 2'); insert into #client values (3, 'User 3'); insert into #transactions values (1, 1, 50); insert into #transactions values (2, 1, 35); insert into #transactions values (3, 1, 25); insert into #transactions values (4, 2, 10); insert into #transactions values (5, 2, 50); insert into #transactions values (6, 1, 35); insert into #transactions values (7, 3, 25); insert into #transactions values (8, 3, 10); insert into #history values (1, 50); insert into #history values (2, 35); insert into #history values (3, 25); insert into #history values (4, 10); insert into #history values (5, 50); insert into #history values (6, 35); insert into #history values (7, 25); insert into #history values (8, 10); select * from #history join #transactions on #history.transid = #transactions.transid join #client on #transactions.clientId = #client.clientId and #history.transid = #transactions.transid
最后一个联接将3个表联接在一起,这在我对编写SQL的了解中是行不通的。我错了吗(可能是这样)
您为什么认为它不起作用?但是查询的最后一点毫无意义,您可以将其取出。这是对3个表的简单连接:
select * from #history join #transactions on #history.transid = #transactions.transid join #client on #transactions.clientId = #client.clientId