如何在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-

  1. ID
  2. WFS001
  3. WFS002
  4. WFS003
  5. WFS005
  6. .
  7. .
  8. .
  9. .

需要创建一个具有自动增量主键的id +一个字符串必须使用php存储该id : my sql必须显示 - p>

  1. ID li>
  2. WFS001 li>
  3. WFS002 li> \ ñ
  4. WFS003 LI>
  5. WFS005 LI>
  6. LI>
  7. LI>
  8. LI> \ n
  9. 。 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                           |
+----------------------------------+