6
通過ONE查詢在MySQL中多次插入後是否可以獲得所有插入的ID? 例(摘要):獲取MySQL中的最後一個插入的IDS
$ids = array();
$parts = array();
$query = 'INSERT INTO `TABLENAME` (`FIELDS`) VALUES';
$items = array(values);
foreach($items as $item){
$parts[] = '('.$item['key1'].','.$item['key2']...','.$item['keyN'].')';
}
$query .= implode(',',$parts);
mysqli_query($link,$query);
$ids = ... // getting ALL inserted IDs
var_dump($ids);
它可以這樣做,在這裏(解決方案之一)。示例(摘要):
$ids = array();
$items = array(values);
foreach($items as $item){
$query ='INSERT INTO `TABLENAME` (`FIELDS`) VALUES('.$item['key1'].','.$item['key2']...','.$item['keyN'].')';
mysqli_query($link,$query);
$ids[] = mysqli_insert_id($link);
}
var_dump($ids);
但是,我想:一個查詢INSERT的所有項目 - 一個查詢獲取所有插入的ID。
UPDATE
我的解決方案(基於問題generate an integer sequence in MySQL)由單一的查詢最後插入ID`s的.Return序列。
$query_inserted_ids = "SELECT @row := @row +1 AS row_id
FROM TABLENAME , (SELECT @row := LAST_INSERT_ID()-1)r
WHERE @row <(
SELECT id
FROM TABLENAME
ORDER BY id DESC
LIMIT 1)";
感謝您的關注!
另請參閱http://stackoverflow.com/questions/16540997/get-all-inserted-ids-when-inserting-multiple-rows-using-a-single-query –