简体   繁体   English

sql - 在没有额外查询的情况下更新时检查行是否存在?

[英]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 bla存在7时,它返回1

But it also returns 1 when 7 in bla don't exists, why???? 但是当bla中的7不存在时它也返回1 ,为什么???? 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 ). 更正式的方法是使用mysql_affected_rows() (参见此处 )。 By default, though, this only shows if the row has actually changed. 但是,默认情况下,这仅显示行实际是否已更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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