id 最高且欄位等於變數的 MySQL UPDATE 查詢?


語法如下

update yourTableName
set yourColumnName1=yourValue where yourColumnName2=yourValue order by yourIdColumnName DESC LIMIT 1;

為理解上述語法,我們建立一個表。建立表的查詢如下

mysql> create table UpdateWithHighestDemo
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserStatus tinyint,
   -> UserRank int
   -> );
Query OK, 0 rows affected (0.61 sec)

使用 insert 命令將一些記錄插入表中。

查詢如下

mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(1,78);
Query OK, 1 row affected (0.12 sec)
mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(0,118);
Query OK, 1 row affected (0.18 sec)
mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(1,223);
Query OK, 1 row affected (0.62 sec)
mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(1,225);
Query OK, 1 row affected (0.12 sec)
mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(0,227);
Query OK, 1 row affected (0.14 sec)
mysql> insert into UpdateWithHighestDemo(UserStatus,UserRank) values(0,230);
Query OK, 1 row affected (0.17 sec)

使用 select 語句顯示錶中的所有記錄。

查詢如下

mysql> select *from UpdateWithHighestDemo;

以下是輸出

+--------+------------+----------+
| UserId | UserStatus | UserRank |
+--------+------------+----------+
| 1      | 1          | 78       |
| 2      | 0          | 118      |
| 3      | 1          | 223      |
| 4      | 1          | 225      |
| 5      | 0          | 227      |
| 6      | 0          | 230      |
+--------+------------+----------+
6 rows in set (0.00 sec)

以下是更新列的查詢

mysql> update UpdateWithHighestDemo
-> set UserStatus=1 where UserRank=230 order by UserId DESC LIMIT 1;
Query OK, 1 row affected (0.19 sec)
Rows matched: 1 Changed: 1 Warnings: 0

讓我們使用 select 語句檢查和顯示錶中的記錄。

查詢如下

mysql> select *from UpdateWithHighestDemo;

以下是輸出

+--------+------------+----------+
| UserId | UserStatus | UserRank |
+--------+------------+----------+
| 1      | 1          | 78       |
| 2      | 0          | 118      |
| 3      | 1          | 223      |
| 4      | 1          | 225      |
| 5      | 0          | 227      |
| 6      | 1          | 230      |
+--------+------------+----------+
6 rows in set (0.00 sec)

現在,如果你希望使用最高 id 進行更新,ORDER BY 子句是很有用的。在上文的示例輸出中,最高的“UserId”=6,UserStatus 為 1。

讓我們將 UserStatus 更新為 0。

查詢如下

mysql> update UpdateWithHighestDemo
   -> set UserStatus=0 order by UserId DESC LIMIT 1;
Query OK, 1 row affected (0.18 sec)
Rows matched: 1 Changed: 1 Warnings: 0

使用 select 語句檢查表中的記錄。

查詢如下

mysql> select *from UpdateWithHighestDemo;
+--------+------------+----------+
| UserId | UserStatus | UserRank |
+--------+------------+----------+
| 1      | 1          | 78       |
| 2      | 0          | 118      |
| 3      | 1          | 223      |
| 4      | 1          | 225      |
| 5      | 0          | 227      |
| 6      | 0          | 230      |
+--------+------------+----------+
6 rows in set (0.00 sec)

更新於:2019 年 7 月 30 日

490 次瀏覽

開始您的 職業

完成課程獲得認證

開始使用
廣告
© . All rights reserved.