I wish to update table1 with any distinct value of table2 matching the same code, no matter which one or the order (value from table2 can’t be picked more than once)
x
+-------------------+ +--------------+
| table1 | | table2 |
+-------------------+ +--------------+
| id | code | value | | code | value |
+----+------+-------+ +------+-------+
| 1 | A | | <-- | A | v1 |
| 2 | A | | | B | v2 |
| 3 | B | | | A | v3 |
+----+------+-------+ | A | v5 |
| A | v6 |
+------+-------+
+-------------------+
| table1 |
+-------------------+
| id | code | value |
+----+------+-------+
| 1 | A | v6 |
| 2 | A | v3 |
| 3 | B | v2 |
+----+------+-------+
How can I write the SQL update statement ? (MySQL 5.7)
Advertisement
Answer
The statement provided by Madhur Bhaiya does work if
- @rn* are initialized to 1 instead of 0 (otherwise row_num* are numbered 1 twice)
- the SELECT from table2 is DISTINCT (because pairs of (code,value) are repeated in table2)
The statement should be
UPDATE
table1 AS t1
JOIN
(SELECT
dt1.id,
IF(@cd1 = dt1.code, @rn1 := @rn1 + 1, 1) AS row_num1,
@cd1 := dt1.code AS code,
FROM (SELECT id, code FROM table1 ORDER BY code, id) AS dt1
CROSS JOIN (SELECT @rn1 := 1, @cd1 := '') AS init1
) AS t2
ON t2.id = t1.id
JOIN
(SELECT
IF(@cd2 = dt2.code, @rn2 := @rn2 + 1, 1) AS row_num2,
@cd2 := dt2.code AS code,
dt2.value
FROM (SELECT DISTINCT code, value FROM table2 ORDER BY code) AS dt2
CROSS JOIN (SELECT @rn2 := 1, @cd2 := '') AS init2
) AS t3
ON t3.row_num2 = t2.row_num1 AND
t3.code = t2.code
SET t1.value = t3.value