Skip to content
Advertisement

SQL statement: how can I pick a value only once from a table to update another table?

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)

How can I write the SQL update statement ? (MySQL 5.7)

Advertisement

Answer

The statement provided by Madhur Bhaiya does work if

  1. @rn* are initialized to 1 instead of 0 (otherwise row_num* are numbered 1 twice)
  2. the SELECT from table2 is DISTINCT (because pairs of (code,value) are repeated in table2)

The statement should be

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement