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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:26:23+00:00 2026-05-20T23:26:23+00:00

I have two tables, Hello and World: Hello has two columns id(int) and id1(int,

  • 0

I have two tables, “Hello” and “World”:

  • “Hello” has two columns id(int) and id1(int, default value 0)

  • “World” has column id2(int)

Whenever id = id2 I would like to +1 to id1.

I tried this using UPDATE like so

UPDATE "Hello" z1 set id1=z1.id1 + 1 FROM "World" z2 WHERE
z1.id = z2.id2

But that changed every column to 1 instead of adding 1 for every occurance of id=id2
After looking around for a solution it seems I need to use an aggregate function but
from previous posts I know that isn’t allowed within UPDATE.

I have a vague idea of one solution where I get the sum for id=id2 per id then put it in a temporary table and then UPDATE based on that value but I can’t seem to put it together so I’m thinking that might not be the best path to a solution?

  • 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-05-20T23:26:24+00:00Added an answer on May 20, 2026 at 11:26 pm

    Here’s what I did:

    Initialization

    create table t1 (id int, n int default 0);
    create table t2 (id int);
    
    insert into t1 values (1), (2), (3);
    insert into t2 values (1), (2), (2), (3), (3), (3);
    

    Tables at the beginning

    t1:

    id ; n
    1  ; 0
    2  ; 0 
    3  ; 0
    

    t2

    id
    1
    2
    2
    3
    3
    3
    

    Test case 1:

    update t1 z1 set n = z1.n + 1 from t2 z2 where z1.id = z2.id;
    

    Result is what you described i.e. t1

    id ; n
    1  ; 1
    2  ; 1 
    3  ; 1
    

    I suspect the reason for this result is that in z1.n + 1 the value of z1.n is determined only at the beginning and it is not updated as more rows match. This way the result is equivalent to the one you get by executing a SELECT select *, t1.n + 1 from t1,t2 where t1.id = t2.id;

    Please note that every UPDATE will increment all values in n by one if there are at least one row for which t1.id = t2.id is true.

    Test case 2:

    update t1 set n = (select count(*) from t2 where t2.id = t1.id)
    

    Result was what you expected i.e. t1

    id ; n
    1  ; 1
    2  ; 2 
    3  ; 3
    

    Please note that the end result is same after each UPDATE if the table t2 has not been modified.

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

Sidebar

Related Questions

Hello i have two columns: duration (int) and dateCol (date) in a table. How
I have two tables that are joined together. A has many B Normally you
I basically have two tables: A(id int, name varchar(10), info varchar(10)) B(id int, item
I have two tables. One of them called conversations has the following data. ConversationID
Hello I have two mysql tables one that holds username and password second that
I basically have two tables: A(id int, name varchar(10), info varchar(10)) B(id int, item
Let's say I have two tables: TABLE A MessageID | Message 1 | Hello
I have two tables, Book and Tag , and books are tagged using the
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables, both with start time and end time fields. I need

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.