Author Topic: Need help with reducing queries  (Read 434 times)

0 Members and 1 Guest are viewing this topic.

Offline ZergmanTopic starter

  • Enthusiast
  • Posts: 242
    • View Profile
Need help with reducing queries
« on: February 20, 2010, 09:20:13 PM »
So I need to retrieve the totals of certain queries from the same DB table.  Basically need to display the total records for certain conditions.

For example, here's part of the queries
Code: [Select]
$query_rsToday1 = "
SELECT data_id
FROM data
WHERE tdate = '$date'
AND resolution = 'Resolved'
";
$rsToday1 = mysql_query($query_rsToday1, $cnepix) or die(mysql_error());
$totalRows_rsToday1 = mysql_num_rows($rsToday1);

$query_rsToday2 = "
SELECT data_id
FROM data
WHERE tdate = '$date'
AND resolution = 'Dispatch'
";
$rsToday2 = mysql_query($query_rsToday2, $cnepix) or die(mysql_error());
$totalRows_rsToday2 = mysql_num_rows($rsToday2);

$query_rsToday3 = "
SELECT data_id
FROM data
WHERE tdate = '$date'
AND level1 = 'Abandon'
";
$rsToday3 = mysql_query($query_rsToday3, $cnepix) or die(mysql_error());
$totalRows_rsToday3 = mysql_num_rows($rsToday3);

I just then display the counts like
Code: [Select]
<?php echo $totalRows_rsToday1?>
There has got to be an easier way to condense the queries but I just can't figure it out.

Offline ZergmanTopic starter

  • Enthusiast
  • Posts: 242
    • View Profile
Re: Need help with reducing queries
« Reply #1 on: February 21, 2010, 06:45:15 PM »
Tried this, but its not working.

Code: [Select]
SELECT level1, level2, resolution, count( * ) AS "total"
FROM `NS_data`
WHERE tdate = '$date'
GROUP BY level1, level2, resolution
ORDER BY level1, level2, resolution

Perhaps i'm not displaying it right

Offline ignace

  • Guru
  • Freak!
  • *
  • Posts: 5,093
  • Gender: Male
    • View Profile
Re: Need help with reducing queries
« Reply #2 on: February 22, 2010, 09:21:36 AM »
$query "SELECT data_id, resolution
FROM data
WHERE tdate = '
$date
AND resolution IN ('Resolved', 'Dispatch')
ORDER BY resolution"
;

$result mysql_query($query);
if (
$result) {
    
$resolved = array();
    
$dispatch = array();
    while (
$row mysql_fetch_assoc($result)) {
        if (
$row['resolution'] === 'Resolved') {
            
$resolved[] = $row;
        } else if (
$row['resolution'] === 'Dispatch') {
            
$dispatch[] = $row;
        }
    }

    
//use $resolved, $dispatch
}
Developer from Belgium, Vlaams-Brabant

Offline ZergmanTopic starter

  • Enthusiast
  • Posts: 242
    • View Profile
Re: Need help with reducing queries
« Reply #3 on: March 02, 2010, 08:28:25 PM »
Great stuff!

Only problem im having is when I use $resolved and $dispatch, the output I get is Array and not the actual value.

I'm stumped