इसके लिए LAST_INSERT_ID() का उपयोग कर सकते हैं।
उदाहरण
टेबल बनाएं:
mysql> create table DemoTable (
-> UserId int(6) unsigned zerofill NOT NULL AUTO_INCREMENT,
-> UserAutoIncrement char(100) default null,
-> PRIMARY KEY(UserId)
-> );
Query OK, 0 rows affected (0.72 sec)
रिकॉर्ड डालें:
mysql> insert into DemoTable values(); Query OK, 1 row affected (0.13 sec)
सभी रिकॉर्ड देखें:
mysql> select * from DemoTable;
आउटपुट
+--------+-------------------+ | UserId | UserAutoIncrement | +--------+-------------------+ | 000001 | NULL | +--------+-------------------+ 1 row in set (0.00 sec)
कन्कैटेनेशन परिणाम वाला कॉलम अपडेट करने की क्वेरी:
mysql> update DemoTable set UserAutoIncrement = CONCAT('USER-', UserId) WHERE UserId = LAST_INSERT_ID();
Query OK, 1 row affected (0.20 sec)
Rows matched: 1 Changed: 1 Warnings: 0
फिर से रिकॉर्ड देखें:
mysql> select * from DemoTable;
आउटपुट
+--------+-------------------+ | UserId | UserAutoIncrement | +--------+-------------------+ | 000001 | USER-000001 | +--------+-------------------+ 1 row in set (0.00 sec)
नोट: LAST_INSERT_ID() अंतिम INSERT द्वारा जनरेट ऑटो_इन्क्रीमेंट मान लौटाता है। ZEROFILL शून्य से पैड करता है।