Edit mysql table

痞子三分冷 提交于 2019-12-25 03:43:40

问题


I want to edit an entire mysql table's row.

I don't really know mysql at all, so in basic programming this is what I want to do:

from row year in table videos:

The key here is that I want to prepend 19/20 and not add it.

if (year < 50)
    year = year+2000
else
    year = year+1900

How would I go about doing this?


回答1:


Try this:

UPDATE yourtable
SET year = year + IF(year >= 50, 1900, 2000)

This will work both if your column is an integer type and also if it is a character type.




回答2:


mysql> create table years (year int);

// populate
mysql> INSERT INTO years VALUES (50);
mysql> INSERT INTO years VALUES (90);
mysql> INSERT INTO years VALUES (40);
mysql> INSERT INTO years VALUES (85);

// update
mysql> UPDATE years SET year = CASE WHEN year < 50 THEN 1900 + year ELSE 2000 + year END;

// check
mysql> SELECT * FROM years;
+------+
| year |
+------+
| 2050 |
| 2090 |
| 1940 |
| 2085 |
+------+


来源:https://stackoverflow.com/questions/5256418/edit-mysql-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!