Making changes to multiple records based on change of single record with SQL

后端 未结 3 845
闹比i
闹比i 2020-11-28 00:17

I have a table of food items. They have a \"Position\" field that represents the order they should appear in on a list (listID is the list they are on, we don\'t want to re-

3条回答
  •  Happy的楠姐
    2020-11-28 00:34

    I do not think this can be conveniently done in less than two queries, which is OK, there should be as few queries as possible, but not at any cost. The two queries would be like (based on what you write yourself)

    UPDATE mytable SET position = 1 WHERE listID = 1 AND name = 'pears';
    UPDATE mytable SET position = position + 1 WHERE listID = 1 AND position BETWEEN 2 AND 4;
    

提交回复
热议问题