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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:50:47+00:00 2026-06-13T04:50:47+00:00

hi i have a table as follow: user table id val1 val2 val3 1

  • 0

hi i have a table as follow:

user table

id  val1 val2 val3
1    2    3    2
2    0    0    1

i would like to know how to count value from columns val1,val2,val3 where id = 1 ? which means
for id = 1 total would be 7 and for id = 2 total would be 1. Most of SO example count the entire columns for all ids.My solution as follow seems not get preferred result

select count(*) as tot
from (
select val1 as total from user
where id=1
union
select val2 as total from user
where id=1
union
select val3 as total from user
where id=1
) as x 

Thanks for help.

  • 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-13T04:50:49+00:00Added an answer on June 13, 2026 at 4:50 am

    It looks like you need to sum the column values for each user id. This will do:

    select id, coalesce(val1, 0) + coalesce(val2, 0) + coalesce(val3, 0) 
    from user
    

    Notice that coalesce is a SQL ANSI defined function, thus, it will work on other DBMSs as well. You can use IFNULL(), but that will only work in MySQL.

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

Sidebar

Related Questions

I have a MySQL table as follow: id, user, culprit, reason, status, ts_register, ts_update
I have a table of user access sessions which records website visitor activity: accessid,
I have a database table for users which contains common fields like password, email
I have a table strcuture (golf score card) as follow: HoleID int CourseID int
I'm trying to follow http://www.foliotek.com/devblog/make-table-rows-sortable-using-jquery-ui-sortable/ And have created a very simple example http://jsfiddle.net/6wbZQ/ It's
I have a table called Follow , with three fields: Id (autoincrement int), UserId
I have one scenario in which the user can define the column and that
I have the models: sales and follow. The table follow has an id, an
hi i have an Mysql table as follow, post_id | user_id | status |
I have a table that contain follow logic. The table display list of names

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.