2

我有两个表,t1每个表t2有两列 -id_userage.
如果. t1.age_ t1.age_ t2.age_ t1.age_t2

更新前:

t1  
+-------+---+   
|id_user|年龄|  
+-------+---+   
| 1| 5|  
+-------+---+   
| 2| 10|  
+-------+---+   
| 3| 10|  
+-------+---+   

t2
+-------+---+   
|id_user|年龄|  
+-------+---+   
| 2| 12|  
+-------+---+   
| 3| 8|  
+-------+---+   
| 4| 20|  
+-------+---+   

更新后:

t1  
+-------+---+   
|id_user|年龄|  
+-------+---+   
| 1| 5|  
+-------+---+   
| 2| 12|  
+-------+---+   
| 3| 10|  
+-------+---+   
4

2 回答 2

2

您可能想尝试:

UPDATE  t1
JOIN    t2 ON (t2.id_user = t1.id_user)
SET     t1.age = t2.age
WHERE   t2.age > t1.age;

测试用例:

CREATE TABLE t1 (id_user int, age int);
CREATE TABLE t2 (id_user int, age int);

INSERT INTO t1 VALUES (1, 5);
INSERT INTO t1 VALUES (2, 10);
INSERT INTO t1 VALUES (3, 10);

INSERT INTO t2 VALUES (2, 12);
INSERT INTO t2 VALUES (3, 8);
INSERT INTO t2 VALUES (4, 20);

结果:

SELECT * FROM t1;
+---------+------+
| id_user | age  |
+---------+------+
|       1 |    5 |
|       2 |   12 |
|       3 |   10 |
+---------+------+
3 rows in set (0.00 sec)
于 2010-07-16T20:29:02.437 回答
0
UPDATE t1
SET age = T2.age
FROM t1
INNER JOIN t2
ON t2.id_user = t1.id_user
WHERE t2.age > t1.age
于 2010-07-16T20:25:33.730 回答