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

  • Home
  • SEARCH
  • 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 8012773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:25:59+00:00 2026-06-04T19:25:59+00:00

Provided that I have the following result set from a mysql database table: +—-+——+——-+

  • 0

Provided that I have the following result set from a mysql database table:

+----+------+-------+
| ID | type | value |
+----+------+-------+
|  8 |    A |  1435 |
|  9 |    B |  7348 | 
| 10 |    A |  1347 | 
| 11 |    A |  3478 | 
| 12 |    A |  4589 | 
| 13 |    B |  6789 |
+----+------+-------+

I would like to delete row ID 8 and push the values in the field ‘value’ down, in such a way that every row has the value of previous entry, but affecting only those where the field ‘type’ is the same as the row being deleted (‘A’ in this case).

That is to say, deleting row id 8 should eventually yield the following:

+----+------+-------+
| ID | type | value |
+----+------+-------+
|  - |    - |    -  | *
|  9 |    B |  7348 |   |
| 10 |    A |  1435 | * |
| 11 |    A |  1347 | * |
| 12 |    A |  3478 | * |
| 13 |    B |  6789 |   V
+----+------+-------+

ID 10 has inherited the value from ID 8, then ID 11 inherits from ID 10, and so on. Notice however how rows having type ‘B’ are unaffected.

So the question: Is there any way to perform this “shift” of values without having to query and update each row one by one? In an ideal world I would do one query to do shift and then another to delete the row, but I’m not quite sure if this is possible at all.

(Also I would rather not use Triggers, since I intend encapsulate all the application logic within the application itself)

  • 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-04T19:26:00+00:00Added an answer on June 4, 2026 at 7:26 pm
    SET @remove_id = 8;
    
    SELECT ID, type, value FROM (
      SELECT   ID,
               type,
               CAST(IF(type <> @type OR ISNULL(@val), value, @val) AS UNSIGNED)
                 AS value,
               @type := IF(ID   = @remove_id, type, @type),
               @val  := IF(type = @type, value, @val)
      FROM     my_table JOIN (SELECT @type := NULL, @val := NULL) AS z
      ORDER BY ID ASC
    ) AS t
    WHERE ID <> @remove_id
    

    See it on sqlfiddle.


    UPDATE

    I hadn’t realised you actually wanted to update the underlying table. For that, you can use some slight hackery to effectively do the same thing in an UPDATE statement (one can’t assign to user variables directly, so instead assign to a column the concatenation of its new value and a null string formed from taking the first 0 characters of the newly assigned user variable):

    SET @remove_id = 8, @type = NULL, @val = NULL;
    
    UPDATE my_table SET
      value = IF(
        type <> @type OR ISNULL(@val),
        value,
        CONCAT(@val, LEFT(@val := value, 0))
      ),
      type = CONCAT(type, LEFT(
        @type := IF(
          ID <> @remove_id,
          @type,
          CONCAT(type, LEFT(@val := value, 0))
        )
      , 0))
    ORDER BY ID ASC;
    
    DELETE FROM my_table WHERE ID = @remove_id;
    

    See it on sqlfiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that somebody from SO kindly provided to solve an issue
I have the following function that draws some data from a chi-squared distribution and
I have a database table that holds parent and child records much like a
I have tried all the solutions that have been provided including using PRAGMA but
I have found that creating a zip file using the Zip task provided by
On a Java portal you can have portlets that include data provided by other
We're using the LightweightBrowserCache provided by RESTEasy 2.2.3.GA but have noticed that when the
I have been provided an XSD and a sample Xml file that contains the
I have a collection that is binded to my Listview. I have provided options
I have the following schema, and would like to do a query that returns

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.