Mysql 在subselect查询中,如何引用顶级select语句的id?
为了测试的目的,我想自动化我的表填充 我需要编辑某个表中的某些列,但我必须确保我在该列中输入的值不只是出于随机性 因此,在一定条件下,这些值实际上来自另一个表 我该怎么做?就像这样的代码:Mysql 在subselect查询中,如何引用顶级select语句的id?,mysql,Mysql,为了测试的目的,我想自动化我的表填充 我需要编辑某个表中的某些列,但我必须确保我在该列中输入的值不只是出于随机性 因此,在一定条件下,这些值实际上来自另一个表 我该怎么做?就像这样的代码: update table_one set `some_id`=(select some_id from another_table where another_table.primary_id=table_one.primary_id order by rand() limit 1) 这有点像我对Subse
update table_one set `some_id`=(select some_id from another_table where another_table.primary_id=table_one.primary_id order by rand() limit 1)
这有点像我对Subselect查询的条件。它应该与我正在更新的当前行的id
匹配。
我现在真的忘记我的SQL了。不过,感谢您的回答。您就快到了-您需要做的就是限定您在子查询中选择的列,这样您就知道它来自正确的表:
update table_one
set some_id=(
select another_table.some_id
from another_table
where another_table.primary_id=table_one.primary_id
order by rand()
limit 1
)