我有2个名为member的表和tree_str ...
“会员”表格如下
self_id parent_id position
100 ROOT ROOT
101 100 LEFT
102 100 CENTRE
103 100 RIGHT
104 101 LEFT
105 101 CENTRE
106 101 RIGHT
“tree_str”表如下....
self_id L1_Left L1_center L1_Right
100
101
102
103
104
105
106
现在我想用这样的第一个方式填充第二张表......
self_id L1_Left L1_center L1_Right
100 101 102 103
101 104 105 106
102
103
104
105
106
................ 我使用查询........
update tree_str
set tree_str.L1_Left = (select member.self_id from tree_str
INNER JOIN member
on member.parent_id=tree_str.self_id
AND member.position='LEFT')
WHERE self_id IN (select member.parent_id from tree_str INNER JOIN member
on member.parent_id=tree_str.self_id and member.position='LEFT')
但它给出了结果.......
Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery
follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
所以plz建议我做正确的查询........... ?????
答案 0 :(得分:1)
为什么不尝试像
这样的东西UPDATE tree_str
SET L1_Left = ml.self_id,
L1_center = mc.self_id,
L1_right = mr.self_id
FROM tree_str t LEFT JOIN
member ml ON t.self_id = ml.parent_id
AND ml.position = 'LEFT' LEFT JOIN
member mc ON t.self_id = mc.parent_id
AND mc.position = 'CENTRE' LEFT JOIN
member mr ON t.self_id = mr.parent_id
AND mr.position = 'RIGHT'
答案 1 :(得分:0)
您的子查询返回多个值的原因是它与正在更新的行不相关。试试下面的SQL。
update ts
set ts.L1_Left =
(select m.self_id
FROM member m
WHERE m.parent_id = ts.self_id
AND m.position='LEFT')
FROM tree_str ts
WHERE self_id IN
(select member.parent_id
from tree_str
INNER JOIN member
on member.parent_id=tree_str.self_id
and member.position='LEFT')
请注意,相关子查询可能很昂贵,因此Astander的答案看起来可能更有效。但是,希望这有助于解释您的原始语法出错的地方。