First to note is however that probably you want to avoid having column names as numeric ... As per "Can a number be used to name a MySQL table column?", I have to quote with a back-tick (a backward quote).
I am actually not sure if you want to do this over multiple rows, but to aggregate over multiple rows - you can do as below
SELECT SUM(CASE WHEN `18` = 'c' THEN 1 ELSE 0 END) AS cnt_18_c FROM badania
So, I am using a CASE statement in my sql - which you can find some more info on "Case statement in MySQL". You could also do it differently as a SUM(CASE...END)
is synonymous to a SUM(IF(...))
statement.
If you would want to do over multiple columns and rows, then you would just add the individual sums you are interested in like in:
SELECT
SUM(CASE WHEN `18` = 'c' THEN 1 ELSE 0 END)
+ SUM(CASE WHEN `17` = 'c' THEN 1 ELSE 0 END)
AS cnt_18_17_c FROM badania
Different groupings (other than all) can by defined with a GROUP BY
. If it is row by row - then the SUM is not even needed.
SELECT
CASE WHEN `18` = 'c' THEN 1 ELSE 0 END
+ CASE WHEN `17` = 'c' THEN 1 ELSE 0 END
AS cnt_18_17_c FROM badania
in the latter case, you can also user smarter techniques. You could concatenate all the tooth as a string, and count occurrences in a string, assuming that the standard set of API's with mysql offers you such a character count. - or you could potentially accomplish the count from within your programming environment (which was PHP). You can also use another smart way to do those character counts.
So I gave a couple of examples - hope I got the MySQL syntax right. Not quote sure exactly what you were asking - I suspect I might have covered it here.