I have this table with values:
val
1
2
3
7
8
9
The table has also an id column, not relevant right now.
I want to insert one value, but the rule is there can’t be any repeated values. Let’s say I insert a new 2, the current 2 should become 3, and so on, up to the value 3, which will turn into 4. In this case, the values 7, 8 and 9 don’t need to be ‘moved’.
I’m working with SQLite. The insert or update part can be done in a separate query, after the desired new value holder is ‘moved’.
So far I was doing this with:
//being n my new value.
UPDATE values SET val = val+1 WHERE val >= n
But this would also modify 7,8 and 9, which don’t need to be modified.
Any thoughts about how to solve this part of the issue? Limiting the update to only the consecutive values?
A better way of doing the whole insert/update in one query would be also very appreciated.
Solution #1 with
LEFT JOIN:http://sqlfiddle.com/#!5/7eeea/12
Solution #2 with
NOT EXISTS:http://sqlfiddle.com/#!5/7eeea/17