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 9224623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:22:09+00:00 2026-06-18T04:22:09+00:00

table Lets call it test —————————- | id | catid | value | |

  • 0

table Lets call it test

 ----------------------------
 |  id  |  catid  |  value  |
 |  1   |    1    |   1     |
 |  2   |    1    |   2     |
 |  3   |    2    |   1     |
 |  4   |    1    |   3     |
 |  5   |    1    |   4     |
 |  6   |    1    |   5     |
 |  7   |    2    |   2     |
 ----------------------------

Suppose i deleted the 2nd row from table. Now the table will become:

 ----------------------------
 |  id  |  catid  |  value  |
 |  1   |    1    |   1     |
 |  3   |    2    |   1     |
 |  4   |    1    |   3     |
 |  5   |    1    |   4     |
 |  6   |    1    |   5     |
 |  7   |    2    |   2     |
 ----------------------------

So in catid1, there is no value 2. so the values will become, 1,3,4,5,…. so far so on….

Goal I need to update the values as subtracting 1 from their previous value so that the continuity is maintined (all values goes one up if the condition is met i.e. if bgger than the deleted value).

CODE
After delete, i am trying to perform an update query.

UPDATE     `test` 
SET        `value` = 
      (

         SELECT t.param FROM (

                         SELECT `value`-1 AS  param
                         FROM `test`
                         WHERE          
                         `catid` = 1 AND `value` > 2

                       ) AS t

       ) 
WHERE 
      `catid` = 1
      AND   `value` > 2 

Now, the most inner query will return all the rows which has value >2 where as the update expects a scalar value of string or numeric.

So the question is how can i return the value in the innermost query with respect to what row is update query targeting?

e.g. If update query is trying to update the row with id 4, the innermost query will retreive the value column with the same row id only and give it back to outer query (after subtracting 1).

Is there any alternative approach in this?

  • 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-18T04:22:11+00:00Added an answer on June 18, 2026 at 4:22 am

    JOIN that table with this subquery like this:

    UPDATE test t1
    INNER JOIN
    ( 
      SELECT 
        id, catid, value - 1 AS value
      FROM test
      WHERE catid = 1
        AND value > 2
    ) AS t2 ON t1.id = t2.id
    SET t1.value = t2.value;
    

    SQL fiddle demo.

    But there is no need for this subquery, you can do this directly like this:

    UPDATE test t1
    SET value = value - 1
    WHERE catid = 1
      AND value > 2;
    


    However, You might need to fix that column id to match those values like this:

    UPDATE test AS t1
    INNER JOIN
    (
      SELECT 
        *, 
        (@rownum := @rownum + 1) AS newID
      FROM test, (SELECT @rownum := 0) AS t
      ORDER BY Id
    ) AS t2 ON t1.id = t2.id
    SET t1.id = t2.newId,
        t1.value = CASE 
                     WHEN t1.catid = 1 AND t1.value > 2 THEN t2.value - 1 
                     ELSE t1.value 
                   END;
    

    Updated SQL Fiddle Demo.

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

Sidebar

Related Questions

I want to send a call to lets say www.test.com/updateMySqlTables.php from my app but
I have couple of rows in database table (lets call it Customer), each row
I have a simple table (lets call it Table1) that has a NVARCHAR field
I have 2 tables which I want to query from. The first table (lets
I have a table to entities (lets call them people) and properties (one person
I have 1 table with 6 columns lets call them A B C D
I have a table with 2 foreign keys, lets call them fk1 and fk2.
Given a class that inherits from ActiveRecord::Base, lets call it Task, I have two
Let's suppose I have the following table (let's call it my_table ): CREATE TABLE
I have a table with a primary key (lets call it person), and another

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.