0

I wanted to insert a row where one of its field got the ID from an AUTO_INCREMENT field. Is it possible to do this in one single query?

INSERT INTO table (`id`, `sameID`) VALUES('', LAST_INSERT_ID())

id | sameID

1 | iam=1

2 | iam=2

3 | this=3

4 | this=4

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Sam San
  • 6,567
  • 8
  • 33
  • 51
  • Maybe this can be done with an INSERT trigger. Why do you want to do this? What is the purpose of the `sameId` column? – Progman Dec 27 '17 at 18:12
  • It is not EQUAL but yes it is SAME. WORDPRESS has 'GUID' field where its value is same as ID. I wanted to create an auto post by following the format of Wordpress. – Sam San Dec 27 '17 at 18:14
  • AFAIK it is not possible with a single statement. At least not with any version I know. – Paul Spiegel Dec 27 '17 at 18:22
  • In your case, the LAST_INSERT_ID() is an expression that is evaluated *before* the INSERT tries to create the row. So it naturally can't get access to the id which will be generated as the row is created. – Bill Karwin Dec 27 '17 at 19:50

0 Answers0