If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > COUNT row but exclude 0

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-10-06, 12:10
mark1491 mark1491 is offline
Registered User
 
Join Date: Mar 2006
Posts: 2
COUNT row but exclude 0

I have a table called 'userrating' with 3 INT fields in it as shown with sample data:

rating
0
3

rating2
2
4

rating3
0
0

I want it to count each row of each field in seperate queries BUT ONLY if there is not a '0' in that row.

So the count of:
rating = 1
rating2 = 2
rating3 = 0

I have a start put i don't know how to check for zeros in the rows, here is what i have for the first field rating:

$ratinginfos = $db->query_read("SELECT *, COUNT(*) AS totalvotes, SUM(rating) AS total FROM " . TABLE_PREFIX . "userrating WHERE userid = $userinfo[userid] AND active=1 GROUP BY userid ORDER BY dateline DESC");


Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 03-10-06, 18:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
the query that you have shown doesn't look anything like the ratings tables

also, you cannot "select star" at the same time as using a GROUP BY

if you don't want to count zeros, here's the way you do it --
Code:
select sum(case when foo = 0 then 0 else 1 end) as non_zero_counts from ...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 03-10-06 at 18:40.
Reply With Quote
  #3 (permalink)  
Old 03-10-06, 21:06
mark1491 mark1491 is offline
Registered User
 
Join Date: Mar 2006
Posts: 2
Quote:
Originally Posted by r937
the query that you have shown doesn't look anything like the ratings tables

also, you cannot "select star" at the same time as using a GROUP BY

if you don't want to count zeros, here's the way you do it --
Code:
select sum(case when foo = 0 then 0 else 1 end) as non_zero_counts from ...
Thanks for the relp, actually the code i pasted came straight from a mod i was using, and does work. But maybe I just don't know what it does? Thanks again.
Reply With Quote
  #4 (permalink)  
Old 03-11-06, 02:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
if the code works, you are lucky, it's pretty bad sql (no offence to you, it's mysql's fault for allowing such awful constructions)

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On