2 Replies Latest reply on Feb 1, 2016 6:28 AM by WolfShade

    MySQL in ColdFusion: after delete of a record, update with new sort order

    WolfShade Level 4

      Hello, all,

       

      I'm trying to automatically update a sort order column after a record has been deleted, so that there are no gaps (yes, I know it still will work with gaps.. my OCD brain cringes at the thought.)

       

      For example:

       

      UUID        |Headline            |Article                    |Active | SortOrder
      ===================================================================================
      ...           This is a headline   This is article text        1       1
      ...           Another headline     Different article text      1       2
      ...           Still yet another    Still different text        1       3
      ...           These are boring     Boring article text         1       4
      

       

      I'm trying to set it so that if the second article (SortOrder is 2) is deleted, the third SortOrder becomes 2, the fourth SortOrder becomes 3.

       

      I've tried (found at this link - apparently it's PHP, not MySQL or ColdFusion):

      SET @x = 0;
      
      UPDATE tableA
      SET SortOrder = (@x:=@x+1)
      ORDER BY SortOrder;
      

       

      But this just gives me a generic "check your MySQL manual for syntax; error where 'Set SortOrder = (@x:=@x+1)' " message.

       

      Is there a way to do this in the SQL without having to use one query to get the SortOrder and another to reset based upon a CF variable?  That's what I've got in place, now, and I hate it.

       

      V/r,

       

      ^_^