0

我有一个名为my_data的表,如下所示:

 +-------+-------+--------+
 | attr1 | attr2 | att3   |
 +-------+-------+--------+
 |   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   |
 +-------+-------+--------+

我需要这样做:

UPDATE my_data 
SET 
    attr3 = 0 
WHERE (attr1, attr2) IN (SELECT attr1, attr2 FROM my_data GROUP BY attr1);

在 mysql 中,更新不适用于其中的 select 语句。还有其他方法吗?

4

2 回答 2

0

您可以使用内部查询加入 my_data 并更新 attr3,如下所示:

UPDATE my_data a
JOIN (SELECT attr1, attr2 FROM my_data GROUP BY attr1) b
ON a.attr1 = b.attr1 AND a.attr2 = b.attr1
SET a.attr3 = 0;
于 2012-12-03T17:10:58.863 回答
0

盯着上面的数据,我猜你想为任何有多个值的更新attr3为 0 。attr1attr2

如果是这种情况,查询应该计算有多少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最小的行将attr2attr3 设置为 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>

试试看 !!!

于 2012-12-03T17:41:44.870 回答