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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:47:33+00:00 2026-05-17T06:47:33+00:00

So the other day, I asked this question about how to combine three complex

  • 0

So the other day, I asked this question about how to combine three complex queries and found a way to do it. Now I’m trying to use those queries to update a field in the users table and can’t find a way to make it work. Here’s the query:

update users set field_sum =(
    select sum(field_sum) from (
        select sum(field_one) as field_sum
          from t_a join t_b on (t_a.bid = t_b.id) where t_b.user_id=users.id 
        union all
        select sum(field_two) as field_sum
          from t_c join t_d on (t_c.did = t_d.id) where t_d.user_id=users.id 
        union all
        select sum(field_three) as field_sum
          from t_e where t_e.user_id=users.id
    ) as field_sumT
)

When I try to run it, I get the following error: ERROR 1054 (42S22): Unknown column 'users.id' in 'where clause'. When I try removing the .user_id=users.id bit from each where clause, it will run but ends up with the total sum of field_sum, not just the field_sum for that user. Is there any way to accomplish 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-05-17T06:47:33+00:00Added an answer on May 17, 2026 at 6:47 am

    Use:

        UPDATE USERS u
    LEFT JOIN (SELECT t_b.user_id,
                      SUM(field_one) as field_sum 
                 FROM t_a 
                 JOIN t_b on t_a.bid = t_b.id
             GROUP BY t_b.user_id) a ON a.user_id = u.id
    LEFT JOIN (SELECT t_d.user_id,
                      SUM(field_two) as field_sum 
                 FROM t_c 
                 JOIN t_d on t_c.did = t_d.id
             GROUP BY t_d.user_id) b ON b.user_id = u.id
    LEFT JOIN (SELECT t_e.user_id,
                      SUM(field_three) as field_sum 
                 from t_e 
             GROUP BY t_e.user_id) c ON c.user_id = u.id
          SET field_num = COALESCE(a.field_sum, 0) + COALESCE(b.field_sum, 0) + COALESCE(c.field_sum, 0)
    

    Caveat

    This will set any users with no records in the supporting rows to have a field_sum value of zero. Do you only want to update those with a record in at least one of those tables?

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

Sidebar

Related Questions

This is related to a question I asked the other day on how to
Someone asked me a question via e-mail about integer partitions the other day (as
I talked to my instructor the other day and asked him this question. He
This question is related to one I asked the other day which I got
This is very similar to a question I asked the other day but my
I asked about Choosing a method to store user profiles the other day and
It's a tricky question I was asked the other day... We're working on a
I saw this the other day (scroll all the way down to see some
I have asked this question on twitter as well the #clojure IRC channel, yet
This question has been asked before but 1) the user never accepted an answer

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.