0

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;
New contributor
Nilesh is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
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
    Jan 14 at 19:41
  • Read the details of ROWS_AFFECTED.
    – Rick James
    Jan 14 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
    Jan 14 at 22:21
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. Jan 14 at 18:45

Your Answer

Nilesh is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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