简体   繁体   English

基于另一个表中的id的MySQL更新表

[英]MySQL update table based on id in another table

This is what I tried 这是我尝试过的

update site
set value_id = 4464551
where (select id from base
where name = 'myName' and
discriminator = 'abc' and 
system_id = 001)

What I want is 我想要的

  • update the value_id in trget site where id = (inner select statement) 在id =(内部select语句)的trget网站中更新value_id

What I got 我得到了什么
- it updated all the rows in site -它更新了站点中的所有行

Question
- how can I get one id that inner select statement returns? -如何获得内部选择语句返回的ID?

The following query should work. 以下查询应该工作。

UPDATE site
SET value_id = 4464551
WHERE site.id = (
    SELECT id 
    FROM base
    WHERE name  'myName' AND 
        discriminator = 'abc' AND 
        system_id = 001
)

Or, if you're expecting multiple rows to be updated: 或者,如果您希望更新多行:

UPDATE site
SET value_id = 4464551
WHERE site.id IN (
    SELECT id 
    FROM base
    WHERE name  'myName' AND 
        discriminator = 'abc' AND 
        system_id = 001
)

You can also get what you want without using inner sql statements, for example: 您还可以在不使用内部sql语句的情况下获得所需的内容,例如:

update site, base
set site.value_id = 4464551
where 
base.name = 'myName' and
base.discriminator = 'abc' and 
base.system_id = 001 and
site.id = base.id

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

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