Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:36:14+00:00 2026-06-07T20:36:14+00:00

I have this table with values: val 1 2 3 7 8 9 The

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:36:17+00:00Added an answer on June 7, 2026 at 8:36 pm

    Solution #1 with LEFT JOIN:

    http://sqlfiddle.com/#!5/7eeea/12

    UPDATE t
    SET val = val+1
    WHERE val >= 2
      AND val <= (
        SELECT MIN(t1.val)
        FROM      t AS t1
        LEFT JOIN t AS t2 ON t1.val+1 = t2.val
        WHERE t1.val >= 2
          AND t2.val IS NULL
      );
    

    Solution #2 with NOT EXISTS:

    http://sqlfiddle.com/#!5/7eeea/17

    UPDATE t
    SET val = val+1
    WHERE val >= 2
      AND val <= (
            SELECT MIN(t1.val)
            FROM  t AS t1
            WHERE t1.val >= 2
              AND NOT EXISTS (SELECT 1 FROM t AS t2 WHERE t1.val+1 = t2.val)
      );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML table as follows. I want retrieve row values from this
I have this table which has seven columns, each column representing a specific value
ENVIRONMENT: C# I have a table of equivalent values, like this: CHEVR = CHEVROLET
I have a table like this one: id group value 1 GROUP A 0.641028
I have a value in my MS SQL table column called XDATA like this:
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have a dynamic table with each row having 4 columns. Column 1 has
I have this table where I can generate dynamic rows (which has input <type=text
I have a table that has 100% width. It is generated dynamically with values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.