开发者

Edit mysql table

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 ke开发者_C百科y 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?


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.


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 |
+------+
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜