2015-06-22 5 views
2

Я пытаюсь получить весь список оставшихся моих ваучеров. Даже это 0.SQL return 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} 

Как я могу получить это?

+0

Вы можете включать выборки данных, которые должны генерировать выходные данные JSON у вас есть выше? –

+0

У меня большой набор данных. и попробуйте запросить это. можете ли вы дать мне предложение, как предоставить вам данные? поэтому я могу экспортировать его для вас. – ssuhat

ответ

0

Причина в следующем формате JSON делает не появляются потому, что не существует соответствующая ей группа (т.е. имеет нулевые записи):

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

Вы можете LEFT JOIN результат из вашего запроса на оригинал sales_voucher таблица для сохранения пустых строк. Я использую COALESCE() для назначения 0, когда значение NULL отображается в столбце voucher_left.

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 
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 
Смежные вопросы