sql - MYSQL IF ELSE? What is the correct statement to use (stock from multiple suppliers) -
i have database in phpmyadmin
suppliers. have created php file exports csv on daily basis cheapest stock , quantity of each unique sku in database. @ moment however, excluding item has no stock anywhere, however, that... if there no stock anywhere print 0 items.
the original sql statement (where name=sku
):
select name,min(price) minprice,quantity products quantity > 0 group name
i tried following, not work intended:
select name,min(price) minprice,if(quantity > 0,quantity,'0') quantity products group name
you want conditional aggregation:
select name, min(price) minprice, coalesce(sum(quantity), 0) quantity products group name;
the problem query where
clause filters out products quantity
0
. seem want these.
i've included coalesce()
in case quantity
can null
. may not needed actual data.
Comments
Post a Comment