问题
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