1

I have below query

select  catid, cat_name, currency, count(is_reporting_category_sales.id) as total_sales, 
sum(total_sales) as total_earning 
from is_category 
  left join is_reporting_category_sales on is_category.catid = is_reporting_category_sales.category_id 
  join is_reporting_order on is_reporting_order.id = is_reporting_category_sales.order_id 
group by catid, cat_name, currency 
ORDER BY `is_category`.`cat_name` ASC

but this is returning only rows that are common in is_category and is_reporting_category_sales, is_reporting_order but I want to fetch all rows from is_category table. And if there is no order for the category then 0 as total_earning and total_sales.

2 Answers 2

1

You have to Use Left Join

left join is_reporting_order on is_reporting_order.id = is_reporting_category_sales.order_id 

Instead of

join is_reporting_order on is_reporting_order.id = is_reporting_category_sales.order_id 
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps using left outer joins you might get the results you expect ( had to guess at some of the aliases for columns btw so some of them might be wrong )

select  c.`catid`, c.`cat_name`, `currency`, count(i.`id`) as 'total_sales', sum(`total_sales`) as 'total_earning' 
from `is_category` c
  left outer join `is_reporting_category_sales` i on c.`catid` = i.`category_id`
  left outer join `is_reporting_order` on o.`id` = i.`order_id`
group by c.`catid`, c.`cat_name`, `currency`
order by c.`cat_name` asc;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.