Forum Moderators: open

Message Too Old, No Replies

Counting ENUM values in SQL query

         

smagdy

6:50 pm on May 22, 2007 (gmt 0)

10+ Year Member



Hello!

I am using php 5 & MySql 5 and i am selecting enum values from DB.

The ENUM is either 'A' or 'B'

So, is it possible to count how much A's or B's directly in the query without using php to loop and count them?

Thanks in advance

physics

9:30 pm on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is one of the reasons I avoid ENUM ;)

But I think you could use a sub select.

SELECT x, y, (SELECT count(ab) FROM yourtable WHERE ab = 'A') as count_A, (SELECT count(ab) FROM yourtable WHERE ab = 'B') as count_B, ...

smagdy

10:07 pm on May 22, 2007 (gmt 0)

10+ Year Member



Ohh!

my query is already big and has some joins, so i wouldnt add this to it!

i think i will go with this...

$sql = mysql_query("Select big query....")

while($rs = mysql_fetch_object($sql))
{
if($rs->enum == "a")
$count++;
}
mysql_data_seek($sql, 0);

or is there better solution?

Thank in advance!