hiya我有一份巨大的工作,试图对数千条记录进行排序,一切正常,但我有一张商业表和一张用户表,现在所有用户都需要应用商业ID。我这样做了:
select b.id as businessid, u.id as userid
from business b, [trade_tools].[dbo].[user] u
where pcode = u.pword AND u.parent_id is null AND u.usertype_id = 5 AND u.display = 1 AND b.display = 1
这很有用,但我想把它包起来说:
update [trade_tools].[dbo].[user]
set parent_id = (select businessid FROM (select b.id as businessid, u.id as userid
from business b, [trade_tools].[dbo].[user] u
where pcode = u.pword AND u.parent_id is null AND u.usertype_id = 5 AND u.display = 1 AND b.display = 1) where userid = currentIDWeoOnNow)
现在我知道这是可能的,我只是没有以正确的方式工作,任何SQL专业版只是在这里借一点,所以我可以让这些数据流动。
我确实尝试使用内部选择运行更新但是这不起作用,因为当你只允许一个时,innrer select创建了2个以上的结果,因此沿着这条路线尝试绕过那个问题。
感谢p.s Server是MSSQL服务器,我正在使用SQL Server Management Studio来运行我的查询。
答案 0 :(得分:1)
在这种情况下,简单地这样做可能会有效:
update [trade_tools].[dbo].[user]
set parent_id = businessid
from business b, [trade_tools].[dbo].[user] u
where pcode = u.pword AND u.parent_id is null AND u.usertype_id = 5 AND u.display = 1 AND b.display = 1
答案 1 :(得分:1)
我认为这次可能会有效::
更新[trade_tools]。[dbo]。[user] set parent_id = b.id
来自business b,[trade_tools]。[dbo]。[user] u其中pcode = u.pword AND u.parent_id为null AND u.usertype_id = 5 AND u.display = 1 AND b.display = 1 and u.userid = currentIDWeoOnNow
答案 2 :(得分:0)
试试这个:
update [trade_tools].[dbo].[user]
set parent_id = b.id
from
business b, [trade_tools].[dbo].[user] u
where pcode = u.pword
AND u.parent_id is null
AND u.usertype_id = 5
AND u.display = 1
AND b.display = 1
where userid =currentIDWeoOnNow