从左联接表获取值不为空的计数

时间:2019-05-21 09:52:27

标签: sql

SELECT o.*, g.goods_tax_price_percent, p.*
  FROM `ecs_order_goods` AS o
     LEFT JOIN `ecs_order_goods` AS p
        ON p.product_id = o.product_id
     LEFT JOIN `ecs_goods` AS g
         ON o.goods_id = g.goods_id
     WHERE o.order_id = '93' ORDER BY goods_id

如何获取g.goods_tax_price_percent的总数不是空值?

我尝试过 SELECT COUNT(g.goods_tax_price_percent IS NOT NULL) AS tax_item 不起作用

我的桌子的例子

---------------------------
TABLE : esc_goods
----------------------------
id | goods_tax_price_percent
----------------------------
1  | NULL
2  | 10
3  | 30
4  | NULL
---------------------------- 

我想要的结果

我要COUNT(g.goods_tax_price_percent IS NOT NULL) AS tax_item = 2

1 个答案:

答案 0 :(得分:0)

您可以使用count()

SELECT COUNT(g.goods_tax_price_percent) as tax_item
FROM ecs_order_goods o LEFT JOIN
     ecs_order_goods p 
     ON p.product_id = o.product_id LEFT JOIN
     ecs_goods g
     ON o.goods_id = g.goods_id
WHERE o.order_id = 93 ;

该查询的LEFT JOIN可能是INNER JOIN