Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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
基于另一个表的id替换名称的Mysql查询_Mysql_Sql - Fatal编程技术网

基于另一个表的id替换名称的Mysql查询

基于另一个表的id替换名称的Mysql查询,mysql,sql,Mysql,Sql,我有两张桌子和两个好名字 id,operator 1 1 2 1 3 1 4 2 5 2 6 3 7 3 表客户端 表1井组名称 id,operator 1 1 2 1 3 1 4 2 5 2 6 3 7 3 我想从clients表中选择id,并更新well_names表中的operator列 预期产量 表1井组名称 id,operator 1 1 2 1 3 1 4 2 5 2 6 3 7 3 此查询将用

我有两张桌子和两个好名字

id,operator
1   1
2   1
3   1
4   2
5   2
6   3
7   3
表客户端

表1井组名称

id,operator
1   1
2   1
3   1
4   2
5   2
6   3
7   3
我想从clients表中选择id,并更新well_names表中的operator列

预期产量

表1井组名称

id,operator
1   1
2   1
3   1
4   2
5   2
6   3
7   3
此查询将用于:

SELECT a.id, b.id 
FROM clients as a LEFT JOIN well_names as b ON a.name = b.operator
可以将子查询与update语句一起使用:

update well_names w
      set operator = (select c.id from Clients c where c.name = w.operator);

相信你的SQL b运算符有一个输入错误,你缺少了oIn SQL。这将失败,因为你不能有两个相同的列名。我建议:从客户端选择a.id,b.id作为运算符作为左连接,以及a.name=b.operator上的b作为名称。我想没有pb;它将被执行。因为请求是显示来自客户机的id,而从well names表中选择的操作员id正在捕获以显示行号,并且当操作员和名称匹配时,来自客户机表的id值将显示在well names id行的前面。当然,我理解你的意思,这是真的,必须是b.id作为操作员
update well_names w
      set operator = (select c.id from Clients c where c.name = w.operator);
UPDATE well_names WN
INNER JOIN Clients C ON WN.operator = C.operator 
set WN.operator = C.id