I have problem with this. What I need is to get unique values in column called order
in my event
table.
How this thing should behave: for example I have table `event
| id | name | order |
|----|---------|-------|
| 1 | walking | 1 |
| 2 | diving | 2 |
| 3 | skating | 3 |
for example when I try to update skating
order to 1
I will get
| id | name | order |
|----|---------|-------|
| 3 | skating | 1 |
| 1 | walking | 2 |
| 2 | diving | 3 |
Have no idea how to do it, tried this but it doesn't work for me:
Yii::$app->db->createCommand("
SET @rownum :=0;
UPDATE " . self::tableName() . "
LEFT JOIN (
SELECT id, (@rownum := @rownum + 1) AS rowNumber
FROM " . self::tableName() . "
ORDER BY order ASC
) as tmp_table
on tmp_table.id = " . self::tableName() . ".id
SET " . self::tableName() . ".order = tmp_table.rowNumber"
)->execute();
If it's not understandable for you, ask me I will try to explain all the things.