盯着上面的数据,我猜你想为任何有多个值的更新attr3
为 0 。attr1
attr2
如果是这种情况,查询应该计算有多少attr2
:
UPDATE
(SELECT attr1,COUNT(attr2) attr2count FROM my_data
GROUP BY attr1 HAVING COUNT(attr2) > 1) A
INNER JOIN my_data B USING (attr1)
SET B.attr3 = 0;
我之所以选择SELECT ... GROUP BY ... HAVING
是因为它更稳定,因为它SELECT attr1, attr2 FROM my_data GROUP BY attr1
只是被复制attr2
的数字之一。attr1
根据 MySQL 文档GROUP BY
,不能保证 mysql 在使用时选择哪个值GROUP BY
。
我在 DBA StackExchange 中回答了一个关于这种有争议的对GROUP BY
.
如果您确定您希望attr1=3
最小的行将attr2
attr3 设置为 0,那么您的问题的答案将是:
UPDATE
(SELECT attr1,MIN(attr2) attr2 FROM my_data
GROUP BY attr1 HAVING COUNT(attr2) > 1) A
INNER JOIN my_data B USING (attr1,attr2)
SET B.attr3 = 0;
让我们测试它们:
测试第一个查询
首先是示例数据:
drop database if exists clickit;
create database clickit;
use clickit
create table my_data (attr1 INT,attr2 INT,attr3 INT);
insert into my_data values
(3,1,1),(4,2,1),(5,3,1),
(3,6,1),(3,7,1),(3,8,1),
(8,8,1),(9,7,1),(3,9,1);
这是加载的示例数据:
mysql> drop database if exists clickit;
Query OK, 1 row affected (0.12 sec)
mysql> create database clickit;
Query OK, 1 row affected (0.00 sec)
mysql> use clickit
Database changed
mysql> create table my_data (attr1 INT,attr2 INT,attr3 INT);
Query OK, 0 rows affected (0.07 sec)
mysql> insert into my_data values
-> (3,1,1),(4,2,1),(5,3,1),
-> (3,6,1),(3,7,1),(3,8,1),
-> (8,8,1),(9,7,1),(3,9,1);
Query OK, 9 rows affected (0.05 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> select * from my_data;
+-------+-------+-------+
| attr1 | attr2 | attr3 |
+-------+-------+-------+
| 3 | 1 | 1 |
| 4 | 2 | 1 |
| 5 | 3 | 1 |
| 3 | 6 | 1 |
| 3 | 7 | 1 |
| 3 | 8 | 1 |
| 8 | 8 | 1 |
| 9 | 7 | 1 |
| 3 | 9 | 1 |
+-------+-------+-------+
9 rows in set (0.00 sec)
mysql>
这是我建议的第一个查询:
mysql> UPDATE
-> (SELECT attr1,COUNT(attr2) attr2count FROM my_data
-> GROUP BY attr1 HAVING COUNT(attr2) > 1) A
-> INNER JOIN my_data B USING (attr1)
-> SET B.attr3 = 0;
Query OK, 5 rows affected (0.10 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from my_data;
+-------+-------+-------+
| attr1 | attr2 | attr3 |
+-------+-------+-------+
| 3 | 1 | 0 |
| 4 | 2 | 1 |
| 5 | 3 | 1 |
| 3 | 6 | 0 |
| 3 | 7 | 0 |
| 3 | 8 | 0 |
| 8 | 8 | 1 |
| 9 | 7 | 1 |
| 3 | 9 | 0 |
+-------+-------+-------+
9 rows in set (0.00 sec)
mysql>
对于具有多行的 attr1,第一个将 attr3 更改为 0
测试第二个查询
让我们重新加载示例并运行第二个查询:
mysql> drop database if exists clickit;
Query OK, 1 row affected (0.04 sec)
mysql> create database clickit;
Query OK, 1 row affected (0.00 sec)
mysql> use clickit
Database changed
mysql> create table my_data (attr1 INT,attr2 INT,attr3 INT
Query OK, 0 rows affected (0.07 sec)
mysql> insert into my_data values
-> (3,1,1),(4,2,1),(5,3,1),
-> (3,6,1),(3,7,1),(3,8,1),
-> (8,8,1),(9,7,1),(3,9,1);
Query OK, 9 rows affected (0.05 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> select * from my_data;
+-------+-------+-------+
| attr1 | attr2 | attr3 |
+-------+-------+-------+
| 3 | 1 | 1 |
| 4 | 2 | 1 |
| 5 | 3 | 1 |
| 3 | 6 | 1 |
| 3 | 7 | 1 |
| 3 | 8 | 1 |
| 8 | 8 | 1 |
| 9 | 7 | 1 |
| 3 | 9 | 1 |
+-------+-------+-------+
9 rows in set (0.00 sec)
mysql> UPDATE
-> (SELECT attr1,MIN(attr2) attr2 FROM my_data
-> GROUP BY attr1 HAVING COUNT(attr2) > 1) A
-> INNER JOIN my_data B USING (attr1,attr2)
-> SET B.attr3 = 0;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from my_data;
+-------+-------+-------+
| attr1 | attr2 | attr3 |
+-------+-------+-------+
| 3 | 1 | 0 |
| 4 | 2 | 1 |
| 5 | 3 | 1 |
| 3 | 6 | 1 |
| 3 | 7 | 1 |
| 3 | 8 | 1 |
| 8 | 8 | 1 |
| 9 | 7 | 1 |
| 3 | 9 | 1 |
+-------+-------+-------+
9 rows in set (0.00 sec)
mysql>
试试看 !!!