简体   繁体   English

MySQL更新字段与来自同一表中另一个字段的值

[英]mysql update field with value from another field in same table

I have a table like this: 我有一张这样的桌子:

----------------------------------
| id | date1      | date2 | type |
----------------------------------
| 1  | NULL       | NULL  | A    |
| 1  | NULL       | NULL  | B    |
| 1  | NULL       | NULL  | A    |
| 1  | NULL       | NULL  | A    |
| 1  | 2016-08-02 | NULL  | C    |
| 1  | NULL       | NULL  | B    |
| 2  | NULL       | NULL  | A    |
| 2  | NULL       | NULL  | A    |
| 2  | NULL       | NULL  | A    |
| 2  | NULL       | NULL  | B    |
| 2  | 2016-08-15 | NULL  | C    |
| 2  | NULL       | NULL  | B    |
----------------------------------

and I would like that become by single query like this: 我希望通过单个查询变成这样:

----------------------------------------
| id | date1      | date2       | type |
----------------------------------------
| 1  | NULL       | 2016-08-02  | A    |
| 1  | NULL       | 2016-08-02  | B    |
| 1  | NULL       | 2016-08-02  | A    |
| 1  | NULL       | 2016-08-02  | A    |
| 1  | 2016-08-02 | NULL        | C    |
| 1  | NULL       | 2016-08-02  | B    |
| 2  | NULL       | 2016-08-15  | A    |
| 2  | NULL       | 2016-08-15  | A    |
| 2  | NULL       | 2016-08-15  | A    |
| 2  | NULL       | 2016-08-15  | B    |
| 2  | 2016-08-15 | NULL        | C    |
| 2  | NULL       | 2016-08-15  | B    |
----------------------------------------

The date to get will be always "type C" and the field to update will be always the same id of the date and "type A" or "type B" 获取日期将始终为“ type C”,而要更新的字段将始终为日期的相同ID,且为“ type A”或“ type B”

Any suggest? 有什么建议吗?

You could use an updated joined on subselect 您可以在子选择上使用更新的联接

 update my_table 
 inner join  ( select id, date1 from my_table where type ='C') t2
 on my_table.id = t2.id
 set date2 = t2.date1
 where type in ('A','B')
UPDATE myTable t1
SET date2 = (SELECT MAX(date1)
             FROM myTable t2 
             WHERE t2.id = t1.id)
WHERE t1.type <> 'C'

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

相关问题 从同一表的其他列更新mysql列字段值 - Update mysql columns field value from other columns of the same table mysql 从另一个表更新表字段 - mysql update table field from another table mysql查询用同一表中其他字段的值更新字段 - mysql query to update field with the value of other field in the same table 使用另一个表中的值更新字段 - UPDATE a field with a value from another table MySQL更新字段来自另一个表中的最新字段 - MySQL update field from latest field in another table MySQL根据另一个表中同一行的另一个字段值的重复外观更新布尔值 - MySQL updating a Boolean field value based on the duplicated appearance of another filed value of the same row from another table 如果返回 True mysql php,如何查询唯一值并更新同一表中的另一个字段 - How Can I Query for Unique Value and Update another Field in Same Table If return True mysql php 当两个表字段在MySQL中具有相同的值时,如何忽略将表中的记录插入到另一个表中? - How to ignore inserting records from a table to another table when both table field has same value in MySQL? 在同一行的同一MySQL表上选择一个字段的值到另一个字段? - Select value of one field into another on the same MySQL table in from the same row? 如何通过使用另一个表中的数据来更新表的字段(MYSQL) - How to update a field of a table by using data from another table(MYSQL)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM