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×ֶεÄʱºòϵͳ²¢²»»á¼Ç¼µ½Õâ¸öº¯ÊýÀï±ß¡£Ö»ÓÐÊÇϵͳ×Ô¶¯ÔöÒ»µÄʱºò²Å»á¼Ç¼£¡±ÈÈ磺һ¸ö±íÓ
Ïà¹ØÎĵµ£º
groupadd mysql
н¨Ò»¸öÃûΪmysqlµÄÓû§×é
useradd -g mysql mysql
ÔÚmysqlÓû§×éÏÂн¨Ò»¸öÃûΪmysqlµÄÓû§
gunzip ½âѹÏÂÔص½µÄ.gzÎļþ
cd mysql-VERSION
½øÈë½âѹºóµÄĿ¼
CFLAGS="-O3 -mcpu=pentium4" CXX=gcc CXXFLAGS="-O3 -march=pentium4 -felide-constructors -fno-exceptions -fno-rtti" ./configu ......
mysql°²×°Í¼½â½Ì³Ì 5.1
2009Äê03ÔÂ01ÈÕ ÐÇÆÚÈÕ 23:51
תÔØÇë×¢Ã÷³ö´¦:http://hi.baidu.com/herdff/blog/item/6e6b4bcf6132f90b93457e14.html
±¾½Ì³ÌÒÔMysql 5.1°æ±¾ÎªÀý£¬¸ü¸ß°æ±¾°²×°¹ý³ÌÓë´ËÀàËÆ¡£ÔĶÁÇ°£¬ÇëÏÂÔØ5.1£¬ÏÂÔصØÖ·£ºwww.mysql.com
°²×°¹ý³ÌÓëÆäËüWindows°²×°³ÌÐòÒ»Ñù£¬Ê×ÏȳöÏÖµÄÊÇ°²×°Ïòµ¼»¶Ó½çÃæ
Ö ......
SQL±ê×¼ÔÚÊý¾Ý´æ´¢µÄÎïÀí·½ÃæûÓÐÌṩ̫¶àµÄÖ¸ÄÏ¡£SQLÓïÑÔµÄʹÓöÀÁ¢ÓÚËüËùʹÓõÄÈκÎÊý¾Ý½á¹¹»òͼ±í¡¢±í¡¢ÐлòÁÐϵĽéÖÊ¡£µ«ÊÇ£¬´ó²¿·Ö¸ß¼¶Êý¾Ý¿â¹ÜÀíϵͳÒѾ¿ª·¢ÁËһЩ¸ù¾ÝÎļþϵͳ¡¢Ó²¼þ»òÕßÕâÁ½ÕßÀ´È·¶¨½«ÒªÓÃÓÚ´æ´¢Ìض¨Êý¾Ý¿éÎïÀíλÖõķ½·¨¡£ÔÚMySQLÖУ¬InnoDB´æ´¢ÒýÇ泤ÆÚÖ§³Ö±í¿Õ¼äµÄ¸ÅÄ²¢ÇÒMySQL·þÎñÆ÷ÉõÖÁÔÚ ......
¶ÔÓÚÿ¸öÀàÐÍÓµÓеÄÖµ·¶Î§ÒÔ¼°²¢ÇÒÖ¸¶¨ÈÕÆÚºÎʱ¼äÖµµÄÓÐЧ¸ñʽµÄÃèÊö¼û7.3.6 ÈÕÆÚºÍʱ¼äÀàÐÍ¡£
ÕâÀïÊÇÒ»¸öʹÓÃÈÕÆÚº¯ÊýµÄÀý×Ó¡£ÏÂÃæµÄ²éѯѡÔñÁËËùÓмǼ£¬Æädate_colµÄÖµÊÇÔÚ×îºó30ÌìÒÔÄÚ£º
mysql> SELECT something from table
WHERE TO_DAYS(NOW())&nbs ......
¶¨ÒåÓαê
DECLARE cur_Ãû³Æ CURSOR
FOR
SELECT ×Ö¶Î1,×Ö¶Î2,...×Ö¶În from ±í where ×Ö¶Î1=±äÁ¿;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET ±äÁ¿1=null,±äÁ¿2=null;
OPEN cur_Ãû³Æ;
FETCH cur_Ãû³Æ INTO ±äÁ¿1,±äÁ¿2...;
WHILE ( ±äÁ¿1 IS NOT NULL) DO
&nbs ......