Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在subselect查询中,如何引用顶级select语句的id?_Mysql - Fatal编程技术网

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
)