查找MIN值> 0,也没有负值

时间:2019-07-04 14:46:37

标签: mysql

我想从MySQL读取一行内容,该行内容提供了我的平均值,std dev pop和max以及mins。

基本上,我想从特定的表中获得MIN,但不是0,或者小于0。

我尝试过

MIN(NULLIF(`stock_qty`, 0)) AS `stock_qty`,

作为SELECT参数,但是如果MIN为0,则仅返回null

我当前的方法如下:

SELECT 
    MAX(`stock_qty`) AS `stock_qty_max`,
    MIN(`stock_qty`) AS `stock_qty_min`,
    AVG(`stock_qty`) AS `stock_qty_avg`,
    STDDEV_POP(`stock_qty`) AS `stock_qty_std`,
    SUM(`stock_qty`) AS `stock_qty_sum`,
    MAX(`adjusted_online_set`) AS `adjusted_online_set_max`,
    MIN(`adjusted_online_set`) AS `adjusted_online_set_min`,
    AVG(`adjusted_online_set`) AS `adjusted_online_set_avg`,
    STDDEV_POP(`adjusted_online_set`) AS `adjusted_online_set_std`,
    SUM(`adjusted_online_set`) AS `adjusted_online_set_sum`,
    COUNT(*) AS `count`
FROM
    `cat_product` AS `prod`
        LEFT JOIN
    `cat_position` AS `pos` ON pos.product_id = prod.product_id
        RIGHT JOIN
    `cat_product_website` AS `pweb` ON pos.product_id = pweb.product_id
WHERE
    (pos.category_id = '8476')
    AND ((prod.adjusted_online_set > 0) OR (prod.stock_qty > 0))

但是,这两个字段也都返回负值。

1 个答案:

答案 0 :(得分:0)

min( 案件     当stock_qty> 0时stock_qty     否则为空 结束 )AS stock_qty