å¨MySQLä¸ï¼ä½¿ç¨auto_incrementç±»åçidå段ä½ä¸ºè¡¨ç主é®ï¼å¹¶ç¨å®ä½ä¸ºå
¶ä»è¡¨çå¤é®ï¼å½¢æâ主ä»è¡¨ç»æâï¼è¿æ¯æ°æ®åºè®¾è®¡ä¸å¸¸è§çç¨æ³ãä½æ¯å¨å
·ä½çæidçæ¶åï¼æ们çæä½é¡ºåºä¸è¬æ¯ï¼å
å¨ä¸»è¡¨ä¸æå
¥è®°å½ï¼ç¶åè·å¾èªå¨çæçidï¼ä»¥å®ä¸ºåºç¡æå
¥ä»è¡¨çè®°å½ãè¿éé¢æ个å°é¾ï¼å°±æ¯æå
¥ä¸»è¡¨è®°å½åï¼å¦ä½è·å¾å®å¯¹åºçidãé常çåæ³ï¼æ¯éè¿âselect max(id) from tablenameâçåæ³ï¼ä½æ¯æ¾ç¶è¿ç§åæ³éè¦èè并åçæ
åµï¼éè¦å¨äºå¡ä¸å¯¹ä¸»è¡¨å 以âXéâï¼å¾
è·å¾max(id)çå¼ä»¥åï¼å解éãè¿ç§åæ³éè¦çæ¥éª¤æ¯è¾å¤ï¼æäºéº»ç¦ï¼èä¸å¹¶åæ§ä¹ä¸å¥½ãæ没ææ´ç®åçåæ³å¢?çæ¡ä¹ä¸æ¯éè¿select LAST_INSERT_ID()è¿ä¸ªæä½ãä¹ä¸çï¼å®åselect max(id)å¾è±¡ï¼ä½å®é
ä¸å®æ¯çº¿ç¨å®å
¨çãä¹å°±æ¯è¯´å®æ¯å
·ä½äºæ°æ®åºè¿æ¥çãä¸é¢éè¿å®éªè¯´æï¼
1ãå¨è¿æ¥1ä¸åA表æå
¥ä¸æ¡è®°å½ï¼A表å
å«ä¸ä¸ªauto_incrementç±»åçå段ã
2ãå¨è¿æ¥2ä¸åA表åæå
¥ä¸æ¡è®°å½ã
3ãç»æï¼å¨è¿æ¥1ä¸æ§è¡select LAST_INSERT_ID()å¾å°çç»æåè¿æ¥2ä¸æ§è¡select LAST_INSERT_ID()çç»ææ¯ä¸åç;èå¨ä¸¤ä¸ªè¿æ¥ä¸æ§è¡select max(id)çç»ææ¯ç¸åçã
å
¶å®å¨MSSQLä¸SCOPE_IDENTITY()åIDENT_CURRENT()çåºå«åè¿éæ¯ç±»ä¼¼çã使ç¨SCOPE_IDENTITY()å¯ä»¥è·å¾æå
¥æ个IDENTITYå段çå½åä¼è¯çå¼ï¼è使ç¨IDENT_CURRENT()ä¼è·å¾å¨æ个IDENTITYå段ä¸æå
¥çæ大å¼ï¼èä¸åºåä¸åçä¼è¯ã
注ï¼ä½¿ç¨select last_insert_id()æ¶è¦æ³¨æï¼å½ä¸æ¬¡æå
¥å¤æ¡è®°å½æ¶ï¼åªæ¯è·å¾ç¬¬ä¸æ¬¡æå
¥çidå¼ï¼å¡å¿
注æ!å¯ä»¥è¯è¯
insert into tb(c1,c2) values (c1value,c2value),(c1value1,c2value2)..ã
温馨提示:答案为网友推荐,仅供参考