0

I am trying to count the amount of times an int or string value is presented in a table row from several columns in a non-normalized table.

SELECT count(*) from persons
WHERE '1' IN (`column 1`, `column 2`, `column 3`,`column 4`) AND person_MyId='14';

What I trying to solve is if it's possible to count the amount of times the value of 1 was located in the four columns without normalizing that table? I'm trying to see what I am missing in my query to pull that off.

1

2 Answers 2

2

Use SUM,

SELECT SUM( (col1 = 1) +  (col2 = 1) +  (col3 = 1) + (col4 = 1) ) AS totCount
FROM persons
WHERE person_MyId = 14;

See example

Sign up to request clarification or add additional context in comments.

2 Comments

this worked flawlessly, thanks Basha, three thumbs up my guy.
@d-comet I am glad it helped you. Please mark as accepted so future readers can benefit from this.
-1

Use query:

SELECT count(*) 
from persons 
WHERE person_MyId='14'
AND (
     column_1 like '%1%'
  OR column_2 like '%1%'
  OR column_3 like '%1%'
  OR column_4 like '%1%'
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.