PDA

View Full Version : Simplying a few MySQL Queries - Help Needed


CommuneZoom
26 Oct 2005, 05:33
I have the following queries that, if possible, I need to simplify. I'd like to knock this down to as few queries as possible as I will need to call additional (probably 2 other items) from the same row.

I am just looking to get the number of rows matching each set, though I'd rather knock 4 queries down to 1-2 if there is another way to do so.


$getpending = $db->query_read("SELECT * FROM son_customers WHERE ispending = '1'");
$totalpending = $db->num_rows($getpending);

$getactive = $db->query_read("SELECT * FROM son_customers WHERE ispending = '0'");
$totalactive = $db->num_rows($getactive);


Is there an alternative, of will I be stuck making 4 seperate calls?

Danny.VBT
26 Oct 2005, 05:48
Try the below, maybe it will work. I need to start coding php more, and stop with vb.net. I'm starting to forget stuff!


$getcustomers = $db->query_read("SELECT COUNT(*) FROM son_customers");
WHILE ($result = $db->fetch_array($getcustomers))
{
if ($result[ispending] == '0')
{
$totalpending = $totalpending + 1
}
else
{
$totalactive = $totalactive + 1
}
}

CommuneZoom
26 Oct 2005, 06:09
Try the below, maybe it will work. I need to start coding php more, and stop with vb.net. I'm starting to forget stuff!


$getcustomers = $db->query_read("SELECT COUNT(*) FROM son_customers");
WHILE ($result = $db->fetch_array($getcustomers))
{
if ($result[ispending] == '0')
{
$totalpending = $totalpending + 1
}
else
{
$totalactive = $totalactive + 1
}
}



Thanks for the reply, however, I need them both to be showing at the same time and not alternating.

As I need it to report the total for the entire table, the above will not work. I have 8 entries in the table and the above reports a blank for active and 1 for pending (when all 8 are pending).

Danny.VBT
26 Oct 2005, 06:12
Thanks for the reply, however, I need them both to be showing at the same time and not alternating.

As I need it to report the total for the entire table, the above will not work. I have 8 entries in the table and the above reports a blank for active and 1 for pending (when all 8 are pending).

Try it without the Count clause. So...


$getcustomers = $db->query_read("SELECT * FROM son_customers");
WHILE ($result = $db->fetch_array($getcustomers))
{
if ($result[ispending] == '0')
{
$totalpending = $totalpending + 1
}
else
{
$totalactive = $totalactive + 1
}
}

Paul M
26 Oct 2005, 08:02
I think the logic may be backwards in that, totalactive should be when ispending = 0.

Try ;

$getcustomers = $db->query_read("SELECT ispending FROM son_customers");
WHILE ($result = $db->fetch_array($getcustomers))
{
if ($result[ispending])
{
$totalpending += 1
}
else
{
$totalactive += 1
}
}

Marco van Herwaarden
26 Oct 2005, 14:43
$getcustomers = $db->query_first("SELECT SUM(IF(ispending = 1, 1, 0)) as pending, SUM(IF(ispending = 0, 1, 0)) as active FROM son_customers"); finished.

Danny.VBT
26 Oct 2005, 18:53
Can you explain this bit?

IF(ispending = 1, 1, 0))

I've never seen this before...

Marco van Herwaarden
26 Oct 2005, 19:43
if the value of ispending is '1', it will result in a value of 1, otherwise 0 (zero), if you then sum it, you will count 1 for each row that have ispending = 1.

By testing ispending = 0, you can reverse the count.

Paul M
26 Oct 2005, 19:50
$getcustomers = $db->query_first("SELECT SUM(IF(ispending = 1, 1, 0)) as pending, SUM(IF(ispending = 0, 1, 0)) as active FROM son_customers"); finished.
Interesting question, is it quicker to use a more complicated SQL query, or a simpler query and a bit of PHP.

Marco van Herwaarden
26 Oct 2005, 22:48
That depends totally on the query. Since this one are simple embedded functions, not used in where/order by/join conditions, i would say the query is much faster then doing it in php.

Danny.VBT
26 Oct 2005, 23:14
if the value of ispending is '1', it will result in a value of 1, otherwise 0 (zero), if you then sum it, you will count 1 for each row that have ispending = 1.

By testing ispending = 0, you can reverse the count.

Thanks Marco :)

AN-net
26 Oct 2005, 23:22
can IF be used within the parameters of where?

Marco van Herwaarden
27 Oct 2005, 06:14
That wouldn't make much sense i think, the where clause is already 1 big if-statement, but i never tried.

more info: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

Danny.VBT
27 Oct 2005, 07:03
/me clicks link

There's so much stuff I haven't even learned yet, Ah! Thanks again Marco. :)

AN-net
27 Oct 2005, 11:39
i was asking because of this:
http://www.vbulletin.org/forum/showthread.php?t=94088