千家信息网

如何解决mysql多个字段update时错误使用and连接字段的问题

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍了如何解决mysql多个字段update时错误使用and连接字段的问题,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。执
千家信息网最后更新 2026年11月02日如何解决mysql多个字段update时错误使用and连接字段的问题

这篇文章主要介绍了如何解决mysql多个字段update时错误使用and连接字段的问题,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

执行语句一

update spoken set book_id = 2 and unit_id = 14 and article_id = 47409 where id = 284989;

结果为只将book_id字段值更新为0,其他字段都没有更改

mysql> select id,book_id,unit_id,article_id from spoken;

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 5 | 55 | 55555 |

+--------+---------+---------+------------+

1 row in set (0.00 sec)

mysql> update spoken set book_id = 2 and unit_id = 14 and article_id = 47409 where id = 284989;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select id,book_id,unit_id,article_id from spoken;

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 0 | 55 | 55555 |

+--------+---------+---------+------------+

1 row in set (0.00 sec)


执行语句二

update spoken set book_id = 2,unit_id = 14,article_id = 47409 where id = 284989;(正常语句)

三个字段值都变更为给定值,

mysql> select id,book_id,unit_id,article_id from spoken;

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 0 | 55 | 55555 |

+--------+---------+---------+------------+

1 row in set (0.00 sec)

mysql> update spoken set book_id = 2,unit_id = 14,article_id = 47409 where id = 284989;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select id,book_id,unit_id,article_id from spoken;

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 2 | 14 | 47409 |

+--------+---------+---------+------------+

1 row in set (0.00 sec)


执行语句三

update spoken set book_id = 2 and unit_id = 14 and article_id = 47409 where id = 284989;

只将第一个字段变更为1

mysql> select id,book_id,unit_id,article_id from spoken;

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 2 | 14 | 47409 |

+--------+---------+---------+------------+

1 row in set (0.00 sec)

mysql> update spoken set book_id = 2 and unit_id = 14 and article_id = 47409 where id = 284989;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select id,book_id,unit_id,article_id from spoken;

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 1 | 14 | 47409 |

+--------+---------+---------+------------+

1 row in set (0.00 sec)

分析,

1、正常的update语法为语句二,更新多个字段的值,多个字段之间使用逗号","分隔。

2、但问题语句一和问题语句三更新多个字段的值使用and ,分隔多个字段;

且语句一将book_id变更为,语句三将book_id变更为1;

一、问题语句一

update spoken set book_id = 2 and unit_id = 14 and article_id = 47409 where id = 284989;

等价于

update spoken set book_id = (2 and unit_id = 14 and article_id = 47409) where id = 284989;

等价于

update spoken set book_id = (2 and (unit_id = 14) and (article_id = 47409)) where id = 284989;

相当于将book_id的值更新为下面语句的值

select 2 and (unit_id = 14) and (article_id = 47409) from spoken where id = 284989;

该语句由三个表达式通过mysql的逻辑运算符and连接

表达式一为: 2

表达式二为:unit_id = 14 (select unit_id = 14 from spoken where id = 284989;)

表达式三为:article_id = 47409 (select article_id = 47409 from spoken where id = 284989;)

由于当时unit_id = 55,article_id=55555

表达一的值为2

表达式二值为0

表达式三的值为0

所以select 2 and (unit_id = 14) and (article_id = 47409) from spoken where id = 284989;

的值为2 and 0 and 0 即为。

即执行语句的结果等价于update spoken set book_id = 0 where id = 284989;

Mysql的逻辑运算

http://www.cnblogs.com/pzk7788/p/6891299.html

逻辑与 ( AND 或 && )

(1) 当所有操作数均为非零值、并且不为 NULL 时,所得值为 1
(2) 当一个或多个操作数为 0 时,所得值为 0
(3) 其余情况所得值为 NULL

mysql> SELECT 1 AND -1, 1 && 0, 0 AND NULL, 1 && NULL ;
+----------+--------+------------+-----------+
| 1 AND -1 | 1 && 0 | 0 AND NULL | 1 && NULL |
+----------+--------+------------+-----------+
| 1 | 0 | 0 | NULL |
+----------+--------+------------+-----------+

二、同理可得语句三

2 and unit_id = 14 and article_id = 47409

相当于将book_id的值更新为下面语句的值

select 2 and (unit_id = 14) and (article_id = 47409) from spoken where id = 284989;

该语句由三个表达式通过mysql的逻辑运算符and连接

表达式一为: 2

表达式二为:unit_id = 14 (select unit_id = 14 from spoken where id = 284989;)

表达式三为:article_id = 47409 (select article_id = 47409 from spoken where id = 284989;)

由于当时unit_id = 14,article_id=47409

表达一的值为2

表达式二值为1

表达式三的值为1

所以select 2 and (unit_id = 14) and (article_id = 47409) from spoken where id = 284989;

的值为2 and 1 and 1 即为1。

即执行语句的结果等价于update spoken set book_id = 1 where id = 284989;

额外的问题:

Mysql如果对mysql的数值型如int做匹配时,unit_id字段和14做匹配时

如下三个语句都匹配到结果

select id,book_id,unit_id,article_id from spoken where unit_id=14;

select id,book_id,unit_id,article_id from spoken where unit_id='14';

select id,book_id,unit_id,article_id from spoken where unit_id='14aaa';

(字符串转数值会截取第一个非数字前面的数字)

mysql> select id,book_id,unit_id,article_id from spoken where unit_id=14;

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 0 | 14 | 47409 |

+--------+---------+---------+------------+

1 row in set (0.00 sec)

mysql> select id,book_id,unit_id,article_id from spoken where unit_id='14';

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 0 | 14 | 47409 |

+--------+---------+---------+------------+

1 row in set (0.00 sec)

mysql> select id,book_id,unit_id,article_id from spoken where unit_id='14aaa';

+--------+---------+---------+------------+

| id | book_id | unit_id | article_id |

+--------+---------+---------+------------+

| 284989 | 0 | 14 | 47409 |

+--------+---------+---------+------------+

1 row in set, 1 warning (0.00 sec)


感谢你能够认真阅读完这篇文章,希望小编分享的"如何解决mysql多个字段update时错误使用and连接字段的问题"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

语句 字段 表达式 多个 问题 更新 三个 等价 篇文章 结果 逻辑 所得 运算 错误 二为 操作数 数值 数字 运算符 之间 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技有限公司 上海挚品互联网科技有限公司 深圳市人人互联网科技有限公司 中国互联网科技股 互联网是不是新科技 互联网为科技 互联网科技战争 互联网金融 科技 杭州互联网科技公司 广西互联网科技公司 长亮科技 互联网金融 深圳前海聚择互联网科技有限公司 金融科技与互联网金融相同和不同 互联网保险科技创业 企晟互联网信息科技有限公司 工业互联网等科技实现智能化赋能 从那以后互联网科技有限公司 萌小萌互联网科技河南有限公司 科技互联网行业投资研究 玉溪新华互联网科技怎么选 互联网科技股 山东特亿宝互联网科技有限公司 狮子座互联网科技培训 撒么互联网科技公司 华脉科技工业互联网产品 汉立互联网科技有限公司联系电话 受益于互联网科技的高速发展 民营互联网公司国防科技 互联网第三次科技革命特征 互联网科技和医药行业
0