'Update NULL fields with last recorded field that has the same ID - mySQL
I need to UPDATE null fields under the colucm "seller_name" with previously recorded seller names that have the same "Product_ID" field. For example given the following table:
| seller_name | Product_ID |
|---|---|
| iYRO ig | 2185564723 |
| Mpiysusu | 2152514625 |
| iYRO ig | 2185564723 |
| oti | 1178042531 |
| NULL | 2152514625 |
| NULL | 2152514625 |
| Mpiysusuyi | 2152514625 |
| NULL | 2152514625 |
| ioKU | 1330811490 |
| Tsug | 595898812 |
| Tsug | 595898812 |
| NULL | 1178042531 |
The final output should be:
| seller_name | Product_ID |
|---|---|
| iYRO ig | 2185564723 |
| Mpiysusu | 2152514625 |
| iYRO ig | 2185564723 |
| oti | 1178042531 |
| Mpiysusu | 2152514625 |
| Mpiysusu | 2152514625 |
| Mpiysusuyi | 2152514625 |
| Mpiysusuyi | 2152514625 |
| ioKU | 1330811490 |
| Tsug | 595898812 |
| Tsug | 595898812 |
| oti | 1178042531 |
I'm new into SQL so any explanation would be appreciated.
Solution 1:[1]
I don't know where the other values came from but if you just want to update the column with the null values you can try to do this.
Update mytable m Join (select seller_name, Product_ID from mytable
group by Product_ID) k
on k.Product_ID = m.Product_ID
set m.seller_name = k.seller_name
where m.seller_name is null
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 |
