我试图更新一个值。
UPDATE val as u JOIN val as s ON u.id = s.id AND s.pd = 'xyz'
SET u.pd = 'wxyz' WHERE s.id = 231
mysqli_affected_rows() // returns 0 if s.pd != xyz
// returns 1 if s.pd = xyz and updates the val
但是当当前val和new val相同时,此查询将受影响的行返回为
UPDATE val as u JOIN val as s ON u.id = s.id AND s.pd = 'xyz'
SET u.pd = 'xyz' WHERE s.id = 231
mysqli_affected_rows() returns 0
如何将此条件设为真?这样它就进入了真正的语句块。感谢。
更新
与PHP代码无关。无论如何它都是这样的
$sql_query = "UPDATE val as u JOIN val as s ON u.id = s.id AND s.pd = 'xyz'
SET u.pd = 'xyz' WHERE s.id = 231";
mysqli_query($con,$sql_query);
if(mysqli_affected_rows($con)) echo 'successfully updated';
else echo 'you have entered wrong value';
答案 0 :(得分:1)
我认为这被认为是MySQL的一个特性,并且在优化方面具有优势。当新值与旧值相同时,不会将任何内容写入磁盘,也不会将任何内容保存到日志中。
如果要强制进行更改,可以在表中添加一个虚拟列,并执行以下操作:
UPDATE val as u JOIN
pd as s
ON u.id = s.id AND s.pd = 'xyz'
SET u.pd = 'xyz',
u.dummy = coalesce(u.dummy, 0) + 1
WHERE s.id = 231;
这会强制改变记录。