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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:48:38+00:00 2026-06-06T17:48:38+00:00

Mysql query question How to count as 1 if is 1,2 or 5 for

  • 0

Mysql query question

How to count as 1 if is 1,2 or 5 for example

field1 = 1
field2= 1
field3= 5
field4= 2
field5=0;
field6=6;

What I need is to check if any of these fields is 1, 2 or 5 and count them as 1 and ignore the rest.

SELECT field1+field2+field3+field4+field5+field6; FROM table WHERE id=1;
// should return 4 (1+1+1+1+0+0)

Could this be done or am I going to have to do it with php?

  • 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-06T17:48:39+00:00Added an answer on June 6, 2026 at 5:48 pm

    You can use a searched CASE statement but as been suggested by @eggyal, there might be a design flaw in your schema. Fixing that could very well simplify your requirement.

    SELECT  CASE WHEN Field1 IN (1, 2, 5) THEN 1 ELSE 0 END
            + CASE WHEN Field2 IN (1, 2, 5) THEN 1 ELSE 0 END
            + CASE WHEN Field3 IN (1, 2, 5) THEN 1 ELSE 0 END
            + CASE WHEN Field4 IN (1, 2, 5) THEN 1 ELSE 0 END
            + CASE WHEN Field5 IN (1, 2, 5) THEN 1 ELSE 0 END
            + CASE WHEN Field6 IN (1, 2, 5) THEN 1 ELSE 0 END
    FROM    Table
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about my mysql query. I want to count the number
In MySQL I have to check whether select query has returned any records, if
This is a follow-up to this previous question: Complicated COUNT query in MySQL .
This question is related to my previous question - MySQL query to show records
So, I'm having some problems with a MySQL query (see other question ), and
I need a mysql query to extract two users who who live on same
I need to get the nid alone in this query. SELECT count(nid) as total,nid
I'm back with another mysql query question. This time, given a time range, I
I have the following MYSQL query: SELECT categories.key, categories.name, count(*) as itemsCount FROM categories
I have a hopefully simple MySQL query question which is eluding me at late

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.