0

I have a PDO statement that insert or update a row depending on a UNIQUE column (if the column exist then update) . I wonder if there is a solution for both to get the id of the INSERTED or UPDATED that I could use in the same query. So ... considering this answer ... how could it be together?

SET @update_id := 0;
UPDATE some_table SET column_name = 'value', id = (SELECT @update_id := id)
WHERE some_other_column = 'blah' LIMIT 1; 
SELECT @update_id;

plus

lastInsertId();

meaning something like (I'm not sure if there would be a sintax for this so I just set what i have in mind:

  IF "INSERT" then SELECT lastInsertId() 
  else if "UPDATE" then select ID of the last row updated;
Community
  • 1
  • 1
MikZuit
  • 684
  • 5
  • 17
  • you can use `mysqli_insert_id($con)` to get the last generated ID if your field is configured by **AUTO_INCREMENT** ... to control the updated rows, start by building SELECT.. no ? – Halayem Anis Jan 04 '16 at 15:48
  • What do you want to do with this? – KhorneHoly Jan 04 '16 at 15:48
  • But why do you want to set the ID to the last inserted ID? If I can understand what you want to do I'll try to help you. But first I need to understand what you want to do – KhorneHoly Jan 04 '16 at 15:56
  • you can ask me anything @KhorneHoly but I don't really get your question. I want to select the last inserted id OR the last inserted updated depending on what happend on the last query. So then maybe after SELECT * FROM table WHERE id = :last_id; – MikZuit Jan 04 '16 at 16:26

0 Answers0