0

I have a database in phpmyadmin for all my suppliers. I have created a php file that exports to csv on a daily basis the cheapest stock and quantity of each unique sku in the database. At the moment however, it is excluding any item that has no stock anywhere, however, i would like that... if there is no stock anywhere to print 0 for those items.

The original sql statement is (where name=SKU):

SELECT name,MIN(price) AS minPrice,quantity FROM products WHERE quantity > 0 GROUP BY name

I tried already the following, but this does not work as intended:

SELECT name,MIN(price) AS minPrice,IF(quantity > 0,quantity,'0') AS quantity FROM products GROUP BY name
4

2 に答える 2

0

条件付き集計が必要です。

SELECT name, MIN(price) AS minPrice, coalesce(sum(quantity), 0) AS quantity 
FROM products
GROUP BY name;

クエリの問題は、 is であるwhereすべての製品を除外する句quantityです0。あなたは実際にこれらを望んでいるようです。

念のため、 を含めましcoalesce()た。実際のデータには必要ない場合があります。quantityNULL

于 2013-08-16T10:57:27.203 に答える