简体   繁体   中英

How can i simplify this php mysql count code and reduce queries?

I have database with 8 different product category for download.

pic, app, ebo, tem, des, cod, mus, cat

I'd like to count clients total downloaded products and total downloads of each product category. Maximum daily limit downloads for category product is 3. When user log in should see how many downloads remain. Here is working code

$query = "SELECT COUNT(*) as sum FROM service_downloads where client_id like '$client'";
$result = mysql_query($query) or die(mysql_error()); 
// Print out result
while($row = mysql_fetch_array($result))
{
echo "You have downloaded". $row['sum'] ." products.";
echo "<br />";
}
$query = "SELECT COUNT(*) as sum FROM service_downloads where client_id like '$client' and product like 'pic'";
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result))
{
echo "". $row['sum'] ." downloaded pictures";
$leftovers = 3 - $row['sum'];
echo " $leftovers pictures remain for download";
echo "<br />";
}
$query = "SELECT COUNT(*) as sum FROM service_downloads where client_id like '$client' and product like 'app'";
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result))
{
echo "". $row['sum'] ."downloaded applications";
$leftovers = 3 - $row['sum'];
echo " $leftovers applications remain for download.";
echo "<br />";
}

$query = "SELECT CO.... This procedure repeat eight times for different product category.

result

You have downloaded 12 products.

3 downloaded pictures 0 pictures remain for download.

1 downloaded applications 2 applications remain for download.

3 downl.......

You could use a GROUP BY statement to group your results.

SELECT   COUNT(`Product`) AS `Sum`, `Product`
FROM     `service_downloads`
WHERE    `client_id` = '<client_id>'
GROUP BY `Product`

Then you can use one while statement to loop through each product:

// Print out result
while($row = mysql_fetch_array($result))
{
    echo "". $row['Sum'] ."downloaded " . $row['Product'];
    $leftovers = 3 - $row['Sum'];
    echo " $leftovers " . $row['Product'] . " remain for download.";
    echo "<br />";
}

You should breakdown the quantity of downloads per category in one query:

SELECT product,COUNT(*)
FROM service_downloads
WHERE client_id like '$client';

I also don't think you need to use LIKE; you probably want to use =

You can get a single result set with all the sums in it with this query.

SELECT COUNT(*) as sum, product
  FROM service_downloads 
 WHERE client_id = '$client' 
   AND PRODUCT IN ('pic', 'app', 'abc', 'def', 'ghi') 
 GROUP BY product WITH ROLLUP
 ORDER BY product NULLS FIRST

This will give you one row for each specific product and a summary (rollup) row with a NULL value in the product column.

If this query takes a long time create an index on (client, product ) and it should go pretty fast.

If you are showing this data frequently, which is what it sounds like, then you should have a separate table that represents those SUMs and is index by CLIENT_ID.

You can then increment/decrement that value each time you add a new entry.

For example, when you add a new row to service_downloads with an entry in 'pic' for CLIENT_ID 1, then you would also increment this shortcut table:

UPDATE service_counts SET pic=pic+1 WHERE client_id=1;
$query = "SELECT COUNT(*) as sum,product FROM service_downloads where client_id like '$client' GROUP BY product";
$result = mysql_query($query) or die(mysql_error()); 
// Print out result
while($row = mysql_fetch_array($result))
{
echo "You have downloaded". $row['sum'] ." ".$row['product'];
echo "<br />";
}

This should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM