条件更新记录mysql查询

时间:2010-12-25 09:39:54

标签: mysql conditional

是否有任何单个msql查询可以更新客户DOB? 我想更新那些DOB大于当前日期的客户的DOB。 例如: - if a customer have dob 2034 update it to 1934 , if have 2068 updated with 1968

我的系统中存在一个错误,如果您输入的日期小于1970年它将其存储为2070年。 这个bug现在已经解决,但是有错误DOB的客户怎么办?所以我必须更新他们的DOB。

所有客户都存储在customer_entity表格中,entity_id是customer_id

详情如下: -

desc customer_entity
    -> ;
+------------------+----------------------+------+-----+---------------------+----------------+
| Field            | Type                 | Null | Key | Default             | Extra          |
+------------------+----------------------+------+-----+---------------------+----------------+
| entity_id        | int(10) unsigned     | NO   | PRI | NULL                | auto_increment | 
| entity_type_id   | smallint(8) unsigned | NO   | MUL | 0                   |                | 
| attribute_set_id | smallint(5) unsigned | NO   |     | 0                   |                | 
| website_id       | smallint(5) unsigned | YES  | MUL | NULL                |                | 
| email            | varchar(255)         | NO   | MUL |                     |                | 
| group_id         | smallint(3) unsigned | NO   |     | 0                   |                | 
| increment_id     | varchar(50)          | NO   |     |                     |                | 
| store_id         | smallint(5) unsigned | YES  | MUL | 0                   |                | 
| created_at       | datetime             | NO   |     | 0000-00-00 00:00:00 |                | 
| updated_at       | datetime             | NO   |     | 0000-00-00 00:00:00 |                | 
| is_active        | tinyint(1) unsigned  | NO   |     | 1                   |                | 
+------------------+----------------------+------+-----+---------------------+----------------+
11 rows in set (0.00 sec)

并且DOB存储在customer_entity_datetime表中,列value包含DOB。但在此表中还存储了所有其他属性的值,例如fname,lname等。因此attribute_id的值为11的是DOB属性。

mysql> desc customer_entity_datetime;
+----------------+----------------------+------+-----+---------------------+----------------+
| Field          | Type                 | Null | Key | Default             | Extra          |
+----------------+----------------------+------+-----+---------------------+----------------+
| value_id       | int(11)              | NO   | PRI | NULL                | auto_increment | 
| entity_type_id | smallint(8) unsigned | NO   | MUL | 0                   |                | 
| attribute_id   | smallint(5) unsigned | NO   | MUL | 0                   |                | 
| entity_id      | int(10) unsigned     | NO   | MUL | 0                   |                | 
| value          | datetime             | NO   |     | 0000-00-00 00:00:00 |                | 
+----------------+----------------------+------+-----+---------------------+----------------+
5 rows in set (0.01 sec)

感谢。

1 个答案:

答案 0 :(得分:1)

类似的东西:

SELECT value_id, value, DATE_SUB(value, INTERVAL 100 YEAR) as 'correct'
FROM customer_entity_datetime
WHERE value > NOW()

应该给你那些行。要更新它们,只需应用:

UPDATE customer_entity_datetime
SET value = DATE_SUB(value, INTERVAL 100 YEAR)
WHERE value > NOW()