hyster Posted November 19, 2010 Share Posted November 19, 2010 im trying to do count from col1 only if col2 = pass this is what ive tried so far. the 1st query i listed is the only 1 that dosent show an error but it only displays col1 and no count. if i remove the part about col2 it works ok. any suggestions? $query = "SELECT make, COUNT(make) and comments = 'pass' FROM $tbl_name GROUP BY make order by count(make) desc"; $query = "SELECT make, COUNT(make) and comments like 'pass' FROM $tbl_name GROUP BY make order by count(make) desc"; $query = "SELECT make, COUNT(make) where comments like 'pass' FROM $tbl_name GROUP BY make order by count(make) desc"; also is it possably to search only for an empty field? ie i have a col that only contains pass or it is empty. id like to search for only the rows that contain an empty field. Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/ Share on other sites More sharing options...
ManiacDan Posted November 19, 2010 Share Posted November 19, 2010 you want a WHERE clause. And don't select make, count(make), just count(make). Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136663 Share on other sites More sharing options...
hyster Posted November 19, 2010 Author Share Posted November 19, 2010 after a few hours of messing i finaly got this working using SELECT make,COUNT(make) from dsgi_serval where comments like 'pass' GROUP BY make order by count(make) desc my next trick is to get a full count next to a where=count IE: 10 rows in make and only 6 are 'pass' so return would be "make = 6/10". do i have to nest the query do do this or is there another way? (i have never nested a query before) Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136896 Share on other sites More sharing options...
ManiacDan Posted November 19, 2010 Share Posted November 19, 2010 SELECT SUM ( IF ( comments = 'pass', 1, 0 ) ) AS withComments, SUM(1) AS total FROM dsgi_serval ORDER BY SUM ( IF ( comments = 'pass', 1, 0 ) ) DESC -Dan Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136909 Share on other sites More sharing options...
hyster Posted November 19, 2010 Author Share Posted November 19, 2010 i used (some statments were missing spaces between them) SELECT SUM ( IF ( comments = 'pass', 1, 0 ) ) AS with Comments, SUM(1) AS total FROM dsgi_serval ORDER BY SUM ( IF ( comments = 'pass', 1, 0 ) ) DESC and it came back with this error """""""""" right syntax to use near 'with Comments, SUM(1) AS total FROM dsgi_serval ORDER BY SU' at line 2 ive never seen a query structered like this and i cannot pick much of it out. Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136917 Share on other sites More sharing options...
mikosiko Posted November 19, 2010 Share Posted November 19, 2010 (some statments were missing spaces between them) funny... but is ok... this: AS withComments, is not the same as: AS with Comments, Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136924 Share on other sites More sharing options...
hyster Posted November 19, 2010 Author Share Posted November 19, 2010 the reason i put the space is that it returns "exel.SUM does not exist " with the space so i thought it was an typo. i used ur code un-alted and i got this use near 'dsgi_servalORDER BY SUM ( IF ( comments = 'pass', 1, 0 ) ) DESC' at line 1 this caught my eye 'dsgi_servalORDER BY so i went through it for other bits that "looked" wrong. i really appreciate your help in this. Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136931 Share on other sites More sharing options...
mikosiko Posted November 19, 2010 Share Posted November 19, 2010 post the complete sentence that you have now Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136939 Share on other sites More sharing options...
hyster Posted November 19, 2010 Author Share Posted November 19, 2010 this is what i have that works. im running it in phpmyadmin atm but ill add the php later. SELECT make,COUNT(make) from dsgi_serval where comments like 'pass' GROUP BY make order by count(make) desc im trying to get the full row count then the row count that contains "pass", so the end result would be. 20 rows in total, 15 contain "pass" "make = 15/20" or "make = 20/15" the sum section i cannot get working. thanks Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136943 Share on other sites More sharing options...
mikosiko Posted November 19, 2010 Share Posted November 19, 2010 post the sentence that was giving to you by Dan showing exactly how you have it right now (the one that is giving you errors).... Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136946 Share on other sites More sharing options...
hyster Posted November 19, 2010 Author Share Posted November 19, 2010 SELECT SUM ( IF ( comments = 'pass', 1, 0 ) ) AS withComments, SUM(1) AS totalFROM dsgi_servalORDER BY SUM ( IF ( comments = 'pass', 1, 0 ) ) DESC error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dsgi_servalORDER BY SUM ( IF ( comments = 'pass', 1, 0 ) ) DESC' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136947 Share on other sites More sharing options...
mikosiko Posted November 19, 2010 Share Posted November 19, 2010 my friend.... you have some spaces where you don't have to (note the spaces after SUM... those are wrong).. and you need spaces in others places (before FROM and ORDER) try: SELECT SUM( IF ( comments = 'pass', 1, 0 ) ) AS withComments, SUM(1) AS total FROM dsgi_serval ORDER BY SUM( IF ( comments = 'pass', 1, 0 ) ) DESC; and even when that statement is correct, I personally I will write it like this, considering that you want to have those counts grouped by make (as you posted in your first SQL): SELECT make, COUNT(*) AS total, SUM(IF(comments = 'pass', 1, 0)) AS withComments FROM dsgi_serval GROUP BY make ORDER BY COUNT(*) DESC; notice that for "total" I did use COUNT only because SUM and COUNT perform a little different... but both will have the same results for that specific field Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136950 Share on other sites More sharing options...
hyster Posted November 19, 2010 Author Share Posted November 19, 2010 thanks miko, that works perfect. Quote Link to comment https://forums.phpfreaks.com/topic/219192-mysql-count/#findComment-1136955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.