-1

I have a table like this:

ID | B1 | B2 | B3 | B4 | B5 |  
------------------------------------  
1  | N  | N  | N  | N  | N  |
2  | N  | Y  | N  | Y  | Y  |
3  | N  | N  | N  | N  | Y  |  

I want a query that should return

ID | Count |  
------------
 1 |   5   |
 2 |   2   |
 3 |   4   |
Parth
  • 9
  • 1

1 Answers1

-1

Note that boolean expression in MySQL resolves into 0/1.

a = b returns 1 only if a is equal to b otherwise 0

SELECT 
  ID,
  (
    (B1 = 'N')+
    (B2 = 'N')+
    (B3 = 'N')+
    (B4 = 'N')+
    (B5 = 'N')
  ) AS Count
FROM your_table

SEE DEMO


Suggestion:

Better normalize your data.

See Normalization in MySQL

Community
  • 1
  • 1
1000111
  • 13,169
  • 2
  • 28
  • 37