0

One single Query to insert multiple rows :

$My query = "INSERT INTO exp_channel_titles(channel_id,author_id,ip_address) VALUES(11,1,'203.109.119.2'),(11,1,'203.109.119.2'),(11,1,'203.109.119.2')";

What I want :

array of auto incremented id.

what i am getting using SELECT LAST_INSERT_ID() :

I get only one single id.What should I do so that I get array of all the auto incremented value of last inserted fields.

P.S : I cant fire multiple insert query.

HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87

2 Answers2

2

if you are using InnoDB on a recent version of MySQL, you can get the list of IDs using LAST_INSERT_ID() and ROW_COUNT().

InnoDB guarantees sequential numbers for AUTO INCREMENT when doing bulk inserts, provided innodb_autoinc_lock_mode is set to 0 (traditional) or 1 (consecutive). Consequently you can get the first ID from LAST_INSERT_ID() and the last by adding ROW_COUNT()-1.

Daan
  • 12,099
  • 6
  • 34
  • 51
1

This gets last X records ordered by ID descreasingly. If your ID is autoincremented, those will be the latest inserted values.

SELECT * FROM exp_channel_titles
ORDER BY id DESC
LIMIT X
Bartłomiej Wach
  • 1,968
  • 1
  • 11
  • 17