简体   繁体   中英

sql - check if row even exist while update without extra query?

echo mysqli_query($con, "UPDATE stuff SET fieldd = 2 WHERE bla = 7");

It returns 1 when 7 exists in bla

But it also returns 1 when 7 in bla don't exists, why???? Is there a way to find out if the update was successfull/if this row exists without checking if this row exists with extra query before?

One method uses variables:

SET @found = 0;
UPDATE stuff
    SET fieldd = if(@found := 1, 2, 2)
    WHERE bla = 7;

SELECT @found;

A more formal way is to use mysql_affected_rows() (see here ). By default, though, this only shows if the row has actually changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM