'update state of duplicate rows in mysql
I want to do this:
| id | desc | status | con |
|---|---|---|---|
| 1 | hi | 2 | b |
| 2 | hi | 2 | b |
| 3 | hell | 2 | b |
| 4 | hell | 2 | b |
| 5 | hell | 3 | c |
Imagin I have above table
First I want to find rows that where = b, then change the status of one row that have the same 'desc' to 1
My result should be like the below table:
| id | desc | status | con |
|---|---|---|---|
| 1 | hi | 2 | b |
| 2 | hi | 1 | b |
| 3 | hell | 2 | b |
| 4 | hell | 1 | b |
| 5 | hell | 3 | c |
Solution 1:[1]
I found the answer:
CREATE VIEW tableView AS SELECT id FROM Table WHERE con = 'b' GROUP BY desc;
UPDATE Table SET `status` = 1 WHERE id IN (SELECT * FROM tableView)
First should create a view and save ids on that, then we can update our status with what id founded.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vahid ghasemi |
