简体   繁体   English

MySQL查询基于另一个表的ID替换名称

[英]Mysql query to replace name based on id of another table

i have two tables clients and well_names 我有两个表客户端和well_names

Table Clients 表客户

id,name
1   ABC
2   TEST
3   XYZ

Table well_names 表well_names

id,operator
1   ABC
2   ABC
3   ABC
4   TEST
5   TEST
6   XYZ
7   XYZ

I want to select id from clients tables and update on operator column in well_names table. 我想从客户端表中选择ID,并在well_names表中的操作符列上进行更新。

Expected output 预期产量

Table well_names 表well_names

id,operator
1   1
2   1
3   1
4   2
5   2
6   3
7   3

This query will work: 此查询将起作用:

SELECT a.id, b.id 
FROM clients as a LEFT JOIN well_names as b ON a.name = b.operator
select w.id as id, 
       c.id as operator
from join tbl_well w 
inner tbl_client c
on c.operator = w.operator

You can use subquery with update statement : 您可以将subquery与update语句一起使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM