Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

mysql - insert autoincrement into second column

I am looking to do a query like so:

id | int | autoincrement something | varchar | 255

insert into `table` set something = concat('val', id);

so that the table ends up looking like

1|val1
2|val2
3|val3...

except that id always ends up being val0 for every row.

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
mysql> describe concattest;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| val   | text             | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> INSERT INTO concattest (val) VALUES (concat('val', LAST_INSERT_ID()));
Query OK, 1 row affected (0.06 sec)

mysql> select * from concattest;
+----+------+
| id | val  |
+----+------+
|  1 | val0 |
+----+------+
1 row in set (0.00 sec)

mysql> INSERT INTO concattest (val) VALUES (concat('val', LAST_INSERT_ID()));
Query OK, 1 row affected (0.00 sec)

mysql> select * from concattest;
+----+------+
| id | val  |
+----+------+
|  1 | val0 |
|  2 | val1 |
+----+------+
2 rows in set (0.00 sec)

mysql> INSERT INTO concattest (val) VALUES (concat('val', LAST_INSERT_ID()));
Query OK, 1 row affected (0.00 sec)

mysql> select * from concattest;
+----+------+
| id | val  |
+----+------+
|  1 | val0 |
|  2 | val1 |
|  3 | val2 |
+----+------+
3 rows in set (0.00 sec)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...