久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

如何解決mysql多個字段update時錯誤使用and連接字段的問題

159次閱讀
沒有評論

共計 5213 個字符,預計需要花費 14 分鐘才能閱讀完成。

這篇文章主要介紹了如何解決 mysql 多個字段 update 時錯誤使用 and 連接字段的問題,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。

執行語句一

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 =
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 =
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)

 

感謝你能夠認真閱讀完這篇文章,希望丸趣 TV 小編分享的“如何解決 mysql 多個字段 update 時錯誤使用 and 連接字段的問題”這篇文章對大家有幫助,同時也希望大家多多支持丸趣 TV,關注丸趣 TV 行業資訊頻道,更多相關知識等著你來學習!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-27發表,共計5213字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 保山市| 麻阳| 清徐县| 乌兰察布市| 钦州市| 通渭县| 信丰县| 澄迈县| 澄城县| 聊城市| 宝丰县| 黄浦区| 巨野县| 元朗区| 当涂县| 宁陕县| 奉化市| 耒阳市| 沙雅县| 黄骅市| 东乡县| 广汉市| 江川县| 合山市| 凤山市| 博野县| 绥芬河市| 马鞍山市| 寿光市| 台州市| 东山县| 江都市| 呼伦贝尔市| 潼关县| 会昌县| 吐鲁番市| 通城县| 普兰店市| 黔江区| 海城市| 淮阳县|