Ravish Kumar Ravish Kumar - 2 months ago 6
MySQL Question

Update mysql columns field value from other columns of the same table

I have a mysql table named

categories

with fields like

Category_id, Category_name, Category_sorted
1 food 0
2 drinks 0
3 milk 0
4 snacks 0
....
2421 other 0


Here I want to update all the
category_id
values into
category_sorted
so that it should be like

Category_id, Category_name, Category_sorted
1 food 1
2 drinks 2
3 milk 3
4 snacks 4
....
2421 other 2421


I can do this with PHP, but I wanted to know if it is possible to update the same with single mysql query.

Answer

this should be pretty straight forward,

UPDATE tableName
SET    category_sorted = category_ID
Comments