[英]SQL SELECT statement inside of FROM, with INNER JOIN
I am writing a stored procedure, inside of which I have some variables and a temp table. 我正在编写一个存储过程,其中包含一些变量和临时表。
I am trying to do the following: 我正在尝试执行以下操作:
INSERT INTO table1(
col1,
col2,
col3,
SELECT
@col1,
t2.col2,
t3.col3
FROM
#table2 t2,
(SELECT *
FROM table4 t4
INNER JOIN #table2 t2 ON t4.ID = t2.ID) t3
However, say t2
and t3
have 5 rows, the above is inserting 5*5=25 rows, where it should just be 5. It seems like a cross product is being computed in the FROM
between t2
and t3
, and I can't quite nail down why. 但是,假设
t2
和t3
有5行,上面插入5 * 5 = 25行,其中它应该只是5.看起来像是在t2
和t3
之间的FROM
中计算的叉积,我不能很明白为什么。
Are you sure you should be using a comma instead of an actual join there? 你确定你应该使用逗号而不是实际的连接吗? That's probably what's causing a cross-product rather than a selection from joined rows.
这可能是导致交叉产品而非连接行选择的原因。
Is this what you intend to do? 这是你打算做的吗?
INSERT INTO table1(col1, col2, col3)
SELECT @col1, t2.col2, t3.col3
FROM table4 t4 INNER JOIN
#table2 t2
ON t4.ID = t2.ID;
Your original query has an extra reference to #table2
. 您的原始查询对
#table2
有额外的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.