MYSQL LAST_INSERT_ID()
LAST_INSERT_ID
×Ô¶¯·µ»Ø×îºóÒ»¸ö INSERT »ò UPDATE ²Ù×÷Ϊ AUTO_INCREMENT ÁÐÉèÖõĵÚÒ»¸ö·¢ÉúµÄÖµ. ²Î¿¼ÕâÀï
The ID that was generated is maintained in the server on a per-connection basis.
LAST_INSERT_IDÊÇ»ùÓÚµ¥¸öconnectionµÄ, ²»¿ÉÄܱ»ÆäËüµÄ¿Í»§¶ËÁ¬½Ó¸Ä±ä¡£
¿ÉÒÔÓà SELECT LAST_INSERT_ID(); ²éѯLAST_INSERT_IDµÄÖµ.
Important: If you insert multiple rows using a single INSERT statement, LAST_INSERT_ID() returns the value generated for the first inserted row only.
ʹÓõ¥INSERTÓï¾ä²åÈë¶àÌõ¼Ç¼, LAST_INSERT_IDÖ»·µ»Ø²åÈëµÄµÚÒ»Ìõ¼Ç¼²úÉúµÄÖµ. ±ÈÈç
mysql> INSERT INTO t VALUES (NULL, 'aaaa'), (NULL, 'bbbb'), (NULL, 'cccc');
mysql> SELECT * from t;
+----+------+
| id | name |
+----+------+
| 1 | Bob |
| 2 | aaaa |
| 3 | bbbb |
| 4 | cccc |
+----+------+
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 2 |
+------------------+
ID 2 ÊÇÔÚ²åÈëµÚÒ»Ìõ¼Ç¼aaaa ʱ²úÉúµÄ.
LAST_INSERT_ID ÊÇÓëtableÎ޹صģ¬Èç¹ûÏò±ía²åÈëÊý¾Ýºó£¬ÔÙÏò±íb²åÈëÊý¾Ý£¬LAST_INSERT_ID»á¸Ä±ä¡£
Ò»°ãÇé¿öÏ»ñÈ¡¸Õ²åÈëµÄÊý¾ÝµÄid£¬Ê¹ÓÃselect max(id) from table ÊÇ¿ÉÒԵġ£
µ«ÔÚ¶àÏß³ÌÇé¿öÏ£¬¾Í²»ÐÐÁË¡£ÔÚ¶àÓû§½»Ìæ²åÈëÊý¾ÝµÄÇé¿öÏÂmax(id)ÏÔÈ»²»ÄÜÓá£
Õâ¾Í¸ÃʹÓÃLAST_INSERT_IDÁË£¬ÒòΪLAST_INSERT_IDÊÇ»ùÓÚConnectionµÄ£¬Ö»ÒªÃ¿¸öÏ̶߳¼Ê¹ÓöÀÁ¢µÄConnection¶ÔÏó£¬LAST_INSERT_IDº¯Êý½«·µ»Ø¸ÃConnection¶ÔAUTO_INCREMENTÁÐ×îеÄinsert or update²Ù×÷Éú³ÉµÄµÚÒ»¸örecordµÄID¡£Õâ¸öÖµ²»Äܱ»ÆäËü¿Í»§¶Ë£¨Connection£©Ó°Ï죬±£Ö¤ÁËÄãÄܹ»ÕÒ»Ø×Ô¼ºµÄ ID ¶ø²»Óõ£ÐÄÆäËü¿Í»§¶ËµÄ»î¶¯£¬¶øÇÒ²»ÐèÒª¼ÓËø¡£
ÊÖ¶¯²åÈëauto_increment×ֶεÄʱºòϵͳ²¢²»»á¼Ç¼µ½Õâ¸öº¯ÊýÀï±ß¡£Ö»ÓÐÊÇϵͳ×Ô¶¯ÔöÒ»µÄʱºò²Å»á¼Ç¼£¡±ÈÈ磺һ¸ö±íÓ
Ïà¹ØÎĵµ£º
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
¸ÃÓï¾äÄÜ´´½¨ÐµÄÊÓͼ£¬Èç¹û¸ø¶¨ÁËOR REPLACE×Ӿ䣬¸ÃÓï¾ä»¹ÄÜÌæ»»ÒÑÓеÄÊÓͼ¡£s ......
MySQL·þÎñÆ÷°üº¬Ò»Ð©ÆäËûSQL DBMSÖв»¾ß±¸µÄÀ©Õ¹¡£×¢Ò⣬Èç¹ûʹÓÃÁËËüÃÇ£¬½«ÎÞ·¨°Ñ´úÂëÒÆÖ²µ½ÆäËûSQL·þÎñÆ÷¡£ÔÚijЩÇé¿öÏ£¬Äã¿ÉÒÔ±àд°üº¬MySQLÀ©Õ¹µÄ´úÂ룬µ«ÈÔ±£³ÖÆä¿ÉÒÆÖ²ÐÔ£¬·½·¨ÊÇÓÓ/*... */”×¢Ê͵ôÕâЩÀ©Õ¹¡£MySQL·þÎñÆ÷Äܹ»½âÎö²¢Ö´ÐÐ×¢ÊÍÖеĴúÂ룬¾ÍÏñ¶Ô´ýÆäËûMySQLÓï¾äÒ»Ñù£¬µ«ÆäËûSQL·þÎñÆ÷½«ºöÂÔ ......
Ö®ËùÒÔдÕâƪ²©¿Í£¬ÊÇÒòΪǰ¼¸Ìì²ÅÅöµ½ÁËÕâôһ¸öÎÊÌ⣬ÒÔÇ°»¹²»ÖªµÀmysqlÓÐÕâôһ¸öÎÊÌâ¡£
ÎÒÃǵÄϵͳʹÓÃÁËstruts+hibernate£¬Êý¾Ý¿âʹÓõÄÊÇmysql£¬×Ö·û¼¯Í³Ò»Îªutf-8¡£
±¾À´ÒѾÍê³ÉÁË£¬µ«ÊÇϵͳʹÓõÄʱºò£¬·¢ÏÖ²é³öÀ´µÄÈËÃûûÓÐ˳Ðò£¬±¾À´ÒÔΪºÜ¼òµ¥£¬ÔÚ²éѯµÄʱºò£¬Ö¸¶¨°´ÕÕnameÅÅÐò¾Í¿ÉÒÔÁË£¬Æñ֪ʵ¼ÊÒ»×ö²Å·¢Ï ......
//linuxϱàÒëg++ $(mysql_config --cflags) ***.cpp $(mysql_config --libs)
/*mysqlÊý¾Ý¿âÖбíµÄÄÚÈÝ
mysql> select * from maindb;
+------------------+---------+-------------+---------+----------+---------------------+---------------------+---------------------+------------------- ......
½ñÌìС±àÒªºÍ´ó¼Ò˵µÄÊÇhandlerµÄÓ÷¨£¬Ï£Íû¶Ô´ó¼ÒÓÐËù°ïÖú¡£
HANDLER tbl_name OPEN [ AS alias ]
HANDLER tbl_name READ index_name { = | >= | <= | < } (value1,value2,...)
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name READ index_name { FIRST | NEXT | PREV | LAST } ......