tibberous Posted September 28, 2007 Share Posted September 28, 2007 I have a table called visitors, and the visitors have different codes. I need to see what percentage have the code 5. Right now I am using: return mysql_num_rows(mysql_query("select code from visitors where code='5'")) / mysql_num_rows(mysql_query("select code from visitors")); But it doesn't seem to work. Does anyone know how else to do this? Quote Link to comment https://forums.phpfreaks.com/topic/71105-seeing-what-percent-of-users-in-a-database-have-the-code-5/ Share on other sites More sharing options...
Barand Posted September 28, 2007 Share Posted September 28, 2007 $res = mysql_query ("SELECT SUM(IF(code=5,1,0)) as fives, COUNT(*) as total FROM visitors"); $percent = mysql_result($res,0,'fives') * 100 / mysql_result($res,0,'total'); Quote Link to comment https://forums.phpfreaks.com/topic/71105-seeing-what-percent-of-users-in-a-database-have-the-code-5/#findComment-357581 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.