简体   繁体   English

如何在MySQL中使用单个选择查询更新多列?

[英]How to update multiple columns with a single select query in MySQL?

Let take a example, there are two tables as bellow. 让我们举个例子,下面有两个表。

OldData
-----------
    id
    name
    address

NewData
-----------
    nid
    name
    address

I want to update OldData table with NewData table. 我想用NewData表更新OldData表。

For that purpose I am trying to use the following query: 为此,我尝试使用以下查询:

UPDATE OldData SET (name, address) = (SELECT name, address FROM NewData WHERE nid = 234)
WHERE id = 123

But it gives a syntax error. 但是它给出了语法错误。

What is the proper way of doing what I try? 做我尝试的正确方法是什么?

UPDATE OldData o, NewData n 
SET n.name = o.name, n.address = o.address 
where n.nid=234 and o.id=123;

尝试这个:

Update oldData set name = (select name from newData where nid = 234),address = (select address from newData where nid = 123);

Try This : 尝试这个 :

UPDATE OldData a,NewData b 
SET a.name = b.name , a.address = b.address 
WHERE a.id=123 AND b.nid = 234

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

相关问题 PHP Mysql 通过单个查询更新具有多行值的多列 - PHP Mysql update multiple columns with multiple row values by single query 如何使用单个mysql查询批量或批量更新具有多个条件或条件的多个字段或列 - How do I bulk or batch update multiple fields or columns with multiple conditions or criteria with a single mysql query 如何在单个MySQL查询中选择多个平均值? - How to select multiple averages in a single MySQL query? 如何通过单个mysql查询更新多个表? - How to update multiple tables by single mysql query? MySQL 在单个查询中使用 PHP 中的 IF ELSE 语句更新多个列 - MySQL Update Multiple Columns with IF ELSE Statement in PHP in single Query MySQL-如何在一个查询中选择多个列? - MySQL - How to SELECT multiple columns in one query? 如何使用选择查询的结果更新一行的多个mysql列? - How can I update multiple mysql columns of a row using the result of a select query? 如何使用单个查询更新mysql中的单列和多行 - how to update single column and multiple rows in mysql using single query 如何在 MySQL 数据库中的 select 子查询中 select 多列? - How to select multiple columns in a select sub-query in MySQL database? 如何在单个mysql更新中使用concat()追加到多列 - How to use concat() to append to multiple columns in a single mysql update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM