即使没有数据,SQL返回0

时间:2015-06-22 05:01:42

标签: mysql sql

我尝试检索所有凭证左侧列表。即使它是0

这是我的SQL:

select v.product_id, v.product_type_id, v.merchant_id, v.merchant_branch_id, 
    count(1) as voucher_left 
from sales_voucher as v 
inner join ( 
    select p.product_id 
    from product as p 
    inner join product_status as s on p.product_id = s.product_id 
    where s.timelimit >= now() ) as a
on v.product_id = a.product_id 
where username is null
group by v.product_id, v.product_type_id, v.merchant_id, v.merchant_branch_id')

以下是结果(JSON格式):

[{"product_id":1622,"product_type_id":2906,"merchant_id":37,"merchant_branch_id":61,"voucher_left":8},
{"product_id":1622,"product_type_id":2906,"merchant_id":37,"merchant_branch_id":342,"voucher_left":4},
{"product_id":1622,"product_type_id":2907,"merchant_id":37,"merchant_branch_id":61,"voucher_left":5},
{"product_id":1622,"product_type_id":2907,"merchant_id":37,"merchant_branch_id":341,"voucher_left":1},
{"product_id":1622,"product_type_id":2907,"merchant_id":37,"merchant_branch_id":342,"voucher_left":3}]

问题是我希望还有一个json:

{"product_id":1622,"product_type_id":2906,"merchant_id":37,"merchant_branch_id":341,"voucher_left":0}

我该如何检索?

2 个答案:

答案 0 :(得分:0)

以下JSON出现的原因是因为没有与之对应的组(即它没有记录):

{"product_id":1622,"product_type_id":2906,"merchant_id":37,"merchant_branch_id":341,
 "voucher_left":0}

您可以LEFT JOIN将查询结果返回到原始sales_voucher表以保留空行。当COALESCE()列中显示NULL值时,我使用voucher_left分配0。

select sv.product_id, sv.product_type_id, sv.merchant_id, sv.merchant_branch_id,
    COALESCE(t.voucher_left, 0)
FROM sales_voucher sv
LEFT JOIN
(
    select v.product_id, v.product_type_id, v.merchant_id, v.merchant_branch_id, 
        count(1) as voucher_left 
    from sales_voucher as v 
    inner join ( 
        select p.product_id 
        from product as p 
        inner join product_status as s on p.product_id = s.product_id 
        where s.timelimit >= now() ) as a
    on v.product_id = a.product_id 
    where username is null
    group by v.product_id, v.product_type_id, v.merchant_id, v.merchant_branch_id')
) t
ON sv.product_id = t.product_id AND sv.product_type_id = t.product_type_id
    AND sv.merchant_id = t.merchant_id, sv.merchant_branch_id = t.merchant_branch_id

答案 1 :(得分:0)

我解决了它

select x.product_id, x.product_type_id, x.merchant_id, x.merchant_branch_id, 
x.total_voucher, ifnull(y.voucher_left, 0) as voucher_left from
(select v.product_id, v.product_type_id, v.merchant_id, v.merchant_branch_id, 
count(1) as total_voucher
from sales_voucher as v 
inner join (
    select p.product_id from product as p inner join product_status as s
    on p.product_id = s.product_id where s.timelimit >= now()
   ) as a on v.product_id = a.product_id
group by v.product_id, v.product_type_id, v.merchant_id, v.merchant_branch_id) as x

left join(
    select v.product_id, v.product_type_id, v.merchant_id, v.merchant_branch_id, 
    count(1) as voucher_left
    from sales_voucher as v 
    inner join (
        select p.product_id from product as p inner join product_status as s
       on p.product_id = s.product_id where s.timelimit >= now()
     ) as a on v.product_id = a.product_id where username is null 
group by v.product_id, v.product_type_id, v.merchant_id, v.merchant_branch_id) as y 
on x.product_id = y.product_id and x.product_type_id = y.product_type_id 
and x.merchant_id = y.merchant_id 
and x.merchant_branch_id = y.merchant_branch_id