How to find total no of '1' in the table?
Hey Friends
Here is the table details and dataid data h1 h2 h3 h4 h5 h6 h7 h8
1 2010-10-10 1 1 1 1 1 1 1 1
1 2010-10-11 1 0 1 0 1 0 0 1
1 2010-10-12 0 1 1 1 1 0 0 0
what i need to know is how can i create query for following
- find total no of zero in h1-h8 for id 1
- find total no of one in h1-h8 for id 开发者_开发知识库1
- find total of data in h1-h8 for id1(total no of zero+total no of one)
- after getting 1,2,3 use the following answer in the equation (val(3)-val(1))/val(3)
i am not that much good in mysql, if you guys can help me then it will be a great help,thks in advance
First of all, it seems like answer to 3. will be 8*number of rows, but...
answer 1:
SELECT SUM (8-(h1+h2+h3+h4+h5+h6+h7+h8)) FROM table WHERE id=YourID
answer 2:
SELECT SUM (h1+h2+h3+h4+h5+h6+h7+h8) FROM table WHERE id=YourID
answer 3:
SELECT SUM (8 as x) FROM table WHERE id=YourID
and answer 4:
figure it out yourself, you should have enough to start your journey now :)
Choosing the right data structure is very important. You can change you table definition to something like this:
id data attribute value
1 2010-10-10 h1 1
1 2010-10-10 h2 1
...
Now you can use simple queries like:
1) SELECT COUNT(*) FROM table WHERE id = 1 AND value = 0
2) SELECT COUNT(*) FROM table WHERE id = 1 AND value = 1
3) SELECT COUNT(*) FROM table WHERE id = 1
精彩评论