Nov*_*Net 1 sql sql-server join
如何连接三个表,如我有三个表productoption,productsize和price和t3没有主键但问题是productsize tbl没有主键
productoption innerjoin productsize innerjoin price
select * from Prices
select * from Product
select * from ProductOption
select * from ProductSize
select PriceFor, SourceID, MainPrice
from Prices
where PriceFor = 2
and SourceID in (select ProductOptionID from ProductOption where ProductId=7)
select * from Product
select ProductID, ProductName +' - '+ Convert(varchar(5),ProductID) as C
from Product
need inner join btw productoption > productsize and productoption > price
Run Code Online (Sandbox Code Playgroud)
缺少主键并不能阻止你进行连接.联接可以是您选择的任何列.以下sytax将加入3个表
SELECT A.*, B.*, C.*
FROM
A
INNER JOIN
B
INNER JOIN
C
ON B.Col1 = C.Col2
ON A.Co3 = B.Col4
Run Code Online (Sandbox Code Playgroud)
但是,如果您没有覆盖连接中使用的列的索引,则性能可能不会很好.我认为你的问题是如何加入3个表?请注意,"ON"的顺序是嵌套的,in-out,表的顺序(在ON条件中)与它们在初始部分join语句中出现的顺序相同.可能会让人困惑