1

I have an update query in MySQL and I want to return true or false based on whether my update query was executed or not. Is there any way we can find whether the update query is executed?

UPDATE table SET table.name = "Anthony" WHERE table.id = 3;
IF (queryUpdated) THEN
    SELECT 1 AS updated;
ELSE
    SELECT 0 AS updated;
END IF;
3
  • 1
    whether my update query was executed or not. ??? If you call this query execution then it was executed (of course if MySQL is accessible). This execution can be successful or may result in some error. If it is sucessful then there can be matched rows or not, if they exists then they can be updated by fact or not... What do you want to know precisely?
    – Akina
    Commented Jan 14, 2022 at 19:41
  • Read the details of ROWS_AFFECTED.
    – Rick James
    Commented Jan 14, 2022 at 22:20
  • What will you then do with the true/false result? If you feed that into another SQL, maybe the two statements can be combined??
    – Rick James
    Commented Jan 14, 2022 at 22:21

1 Answer 1

2

I suspect you want the ROW_COUNT() function

UPDATE table SET table.name = "Anthony" WHERE table.id = 3;
SELECT CASE WHEN ROW_COUNT() > 0 THEN 1 ELSE 0 END AS updated;
1
  • 1
    Note ROW_COUNT() returns 0 if there was no row matching id=3 or also if there was a matching row, but the name on that row was already 'Anthony'. Either way, the update accomplished no net change. Commented Jan 14, 2022 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.