开发者

Prefix SQL database entries

Let's just say I've got an SQL database with 10 entries in it.

id  |  code   |  name
-------------------------
1   |  00001  |  Entry 1
2   |  00002  |  Entry 2
3   |  00003  |  Entry 3
4   |  00004  |  Entry 4
5   |  00005  |  Entry 5
6   |  00006  |  Entry 6
7   |  00007  |  Entry 7
8   |  00008  |  Entry 8
9   |  00009  |  Entry 9
10  |  00010  |  Entry 10

I want to be able to prefix the code entries of all lines from id's 1-5 with pre1. and all lines from id's 6-10 with pre2., so I end up with this:

id  |  code         |  name
-------------------------------
1   |  pre1.00001   |  Entry 1
2   |  pre1.00002   |  Entry 2
3   |  p开发者_开发百科re1.00003   |  Entry 3
4   |  pre1.00004   |  Entry 4
5   |  pre1.00005   |  Entry 5
6   |  pre2.00006   |  Entry 6
7   |  pre2.00007   |  Entry 7
8   |  pre2.00008   |  Entry 8
9   |  pre2.00009   |  Entry 9
10  |  pre2.00010   |  Entry 10

Can this be done? Preferably I'd like to use phpMyAdmin or MySQL Workbench but am happy to consider any ways.


UPDATE 
  yourTable 
SET 
  code = CONCAT(
    IF(id BETWEEN 1 AND 5,
       'pre1.',
       'pre2.'),
    code);

Or more general case

UPDATE 
  yourTable 
SET 
  code = CONCAT('pre1',code)
WHERE
  id BETWEEN 1 AND 5; 

and repeat for other ranges.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜