I am trying to do a query in MySQL, I have a table that contains the following:
- table:
id, ano, m1, m2, m3, m4, m5, m6, m7
- data:
10, 2016, I, P, L, I, L, P, I
In other words, what I want to achieve is that it counts me when it is specifically the ano
and the id
, that is, the example only has data for the ano
2016
and in the following query example I do it to ano
2015
and it tells me anyway the I
in the table calf
query I make:
SELECT id, COUNT(*) AS r
FROM `calf`
WHERE ano='2015' AND id='10' AND m1='I' OR m2='I' OR m3='I' OR m4='I' OR m5='I' OR m6='I' OR m7='I'
The problem is the condition you are using. The select can be interpreted as:
That is, if any of the conditions of the fields is met
m
, it is enough for you to select the record.On the other hand, grouping in this way you can solve it:
Add those
I
that appear by column, provided that it meetsano
andid
: