如何在Mysql中创建一个id(自动增量)与字符串连接? 仅使用PHP
问题描述:
need to create a id having primary key with auto incrementation + a string has to be stored with that id using php like: my sql has to show-
- ID
- WFS001
- WFS002
- WFS003
- WFS005
- .
- .
- .
- .
需要创建一个具有自动增量主键的id +一个字符串必须使用php存储该id : my sql必须显示 - p>
- ID li>
- WFS001 li>
- WFS002 li> \ ñ
- WFS003 LI>
- WFS005 LI>
- LI>
- LI>
- LI> \ n
- 。 li> ol> div>
答
An option is to have an auto increment column, do the insert, get the last id with LAST_INSERT_ID and make an update using another column and the last insert id + string.
答
You can use combination of CONCAT
, LAST_INSERT_ID
and LPAD
functions.
Example with SELECT:
SELECT CONCAT( 'WFS', LPAD(LAST_INSERT_ID(), 2, '0'));
Samples:
mysql> SELECT CONCAT( 'WFS', LPAD( 6, 3, '0'));
+----------------------------------+
| CONCAT( 'WFS', LPAD( 6, 3, '0')) |
+----------------------------------+
| WFS006 |
+----------------------------------+