[英]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 我想要的是
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.