我有以下结构的表:
TBL1
COL1 COL2 COL2
---- ---- ----
A B 1
B C 3
A C 11
A D 13
B D 10
如何更新col3如果col1中的值是重复的?
我希望col3中的值可以使用找到的最大值进行更新。
结果表看起来像:
COL1 COL2 COL2
---- ---- ----
A B 13
B C 10
A C 13
A D 13
B D 10
提前致谢!!!
答案 0 :(得分:1)
您可以在子查询中从COL3中选择最高值,并使用子查询中的值对表执行更新查询
UPDATE TBL1 SET COL3 = (SELECT COL3 FROM TBL1 WHERE COL1 = 'A' ORDER BY COL3 DESC LIMIT 0,1) AS a WHERE COL1 = 'A'
答案 1 :(得分:1)
使用所需数据加入更新。 Correlated subqueries are not wellcome:
update T inner
join ( select c1, max( c3) as m from T) T2
on T.c1 = T2.c1
set T.c3 = T2.m;
测试:
mysql> create table T ( c1 char(1), c3 int ) ;
Query OK, 0 rows affected (0.15 sec)
mysql> insert into T values ( 'A', 1),('B',3),('A',11),('A',13);
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * From T;
+------+------+
| c1 | c3 |
+------+------+
| A | 1 |
| B | 3 |
| A | 11 |
| A | 13 |
+------+------+
mysql> update T inner join ( select c1, max( c3) as m from T) T2
on T.c1 = T2.c1 set T.c3 = T2.m;
Query OK, 2 rows affected (0.07 sec)
Rows matched: 3 Changed: 2 Warnings: 0
mysql> select * from T;
+------+------+
| c1 | c3 |
+------+------+
| A | 13 |
| B | 3 |
| A | 13 |
| A | 13 |
+------+------+
4 rows in set (0.00 sec)
答案 2 :(得分:1)
首先,让我们加载您的样本数据
mysql> drop database if exists dilyan_kn;
Query OK, 1 row affected (0.04 sec)
mysql> create database dilyan_kn;
Query OK, 1 row affected (0.00 sec)
mysql> use dilyan_kn
Database changed
mysql> create table TBL1
-> (col1 char(1),col2 char(1),col3 int);
Query OK, 0 rows affected (0.11 sec)
mysql> insert into TBL1 values
-> ( 'A' , 'B' , 1 ),
-> ( 'B' , 'C' , 3 ),
-> ( 'A' , 'C' , 11 ),
-> ( 'A' , 'D' , 13 ),
-> ( 'B' , 'D' , 10 );
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from TBL1;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| A | B | 1 |
| B | C | 3 |
| A | C | 11 |
| A | D | 13 |
| B | D | 10 |
+------+------+------+
5 rows in set (0.00 sec)
mysql>
在问题中查看您想要的输出,看起来您想要任何给定col1的col3值最高。
实施例
对于col1 = A,您有不同的值1,11和13. 13是最高的
对于col1 = B,您有不同的值3和10. 10是最高
您需要一个子查询,它可以找到任何给定col1的最高col3值。
以下是该查询:
SELECT col1,MAX(col3) maxcol3
FROM TBL1 GROUP BY col1;
让我们运行该子查询
mysql> SELECT col1,MAX(col3) maxcol3
-> FROM TBL1 GROUP BY col1;
+------+---------+
| col1 | maxcol3 |
+------+---------+
| A | 13 |
| B | 10 |
+------+---------+
2 rows in set (0.00 sec)
mysql>
让我们使用此子查询来对整个表进行JOIN,并在子查询的col1列与表的col1列匹配时更新col3列。这是查询:
UPDATE
(
SELECT col1,MAX(col3) maxcol3
FROM TBL1 GROUP BY col1
) A
INNER JOIN TBL1 B USING (col1)
SET B.col3 = A.maxcol3;
让我们运行UPDATE JOIN查询并选择所有TBL1
mysql> UPDATE
-> (
-> SELECT col1,MAX(col3) maxcol3
-> FROM TBL1 GROUP BY col1
-> ) A
-> INNER JOIN TBL1 B USING (col1)
-> SET B.col3 = A.maxcol3;
Query OK, 3 rows affected (0.05 sec)
Rows matched: 5 Changed: 3 Warnings: 0
mysql> select * from TBL1;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| A | B | 13 |
| B | C | 10 |
| A | C | 13 |
| A | D | 13 |
| B | D | 10 |
+------+------+------+
5 rows in set (0.00 sec)
mysql>
任务完成!!!
5行匹配但只有3个更改的原因源于(col1,col3)为('A',13)和('B',10)的行已经具有最大值并且没有不需要改变。