2011-08-29 44 views
-2

可能重複:
mySQL return index after insert後插入MySQL的檢索ID

海蘭,

有沒有一種方法,在mysql數據庫(INSERT INTO table_name VALUES() ...)將東西取回的ID後插入進一步操作?

舊的方法是進行插入,然後在表名上查詢SELECT。但有沒有辦法在單個查詢中做到這一點?

非常感謝您

+1

重複:http://stackoverflow.com/questions/2666277/mysql-return-index-after-insert http://stackoverflow.com/questions/5667614/mysql-last-insert-id-and-found-rows http://stackoverflow.com/questions/2266604/select-last-insert-id –

回答

3

在PHP中,調用mysql_insert_id()。或者直接在MySQL中調用LAST_INSERT_ID()函數。

// PHP 
echo mysql_insert_id(); 

// PHP MySQLi 
echo $mysqli->insert_id; 

-- in MySQL 
SELECT LAST_INSERT_ID(); 
+0

我使用' mysqli'函數... – pufos

+0

問題是..在那一刻可能有很多插入...我只想要ID f從那個特定的插入。 – pufos

+0

@pufos看到另外 –