简体   繁体   中英

How to use mysql query for all value of my table?

I have a MySQL query for ask category quantity to Prestashop, but I cannot extend it to all categories.

I use categories value from $_POST variable, but I'd like to extend it to all categories value:

$query_quant = "SELECT SUM(ps_stock_available.quantity) as tot FROM ps_stock_available LEFT JOIN ps_category_product ON ps_stock_available.id_product = ps_category_product.id_product WHERE ps_category_product.id_category = '$cat'";

Maybe I need to use mysql_fetch_array , but I want to know how can I assign all category value to $cat ?


I tried to use this:

$query_category = "SELECT id_category FROM ps_category";

$result_category = mysql_query($query_category);


while ($row = mysql_fetch_array($result_category))
{

$categoria = $row['id_category'];

But I have error 404

Simply use in statement to pass all categories into your query

    $query_quant = "SELECT SUM(ps_stock_available.quantity) as tot FROM ps_stock_available 
LEFT JOIN ps_category_product ON ps_stock_available.id_product = ps_category_product.id_product WHERE 
ps_category_product.id_category in ('$cat1','$cat2','$cat3','$cat4')";

As you mentioned in comment if you want to have the sum for each category use group by

    $query_quant = "SELECT SUM(ps_stock_available.quantity) as tot,ps_category_product.id_category as cat FROM ps_stock_available 
LEFT JOIN ps_category_product ON ps_stock_available.id_product = ps_category_product.id_product WHERE 
ps_category_product.id_category in ('$cat1','$cat2','$cat3','$cat4') group by ps_category_product.id_category";

Sir if the categories coming as comma separate you can use "IN" cause for checking category ids. Right now in your query you are only using "=" condition for category It means only one category will be extracted. I hope you get it

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