tags:

views:

42

answers:

2

setup:

mysql> create table product_stock(
       product_id integer, qty integer, branch_id integer);
Query OK, 0 rows affected (0.17 sec)

mysql> create table product(
       product_id integer, product_name varchar(255));
Query OK, 0 rows affected (0.11 sec)

mysql> insert into product(product_id, product_name) 
       values(1, 'Apsana White DX Pencil');
Query OK, 1 row affected (0.05 sec)

mysql> insert into product(product_id, product_name) 
       values(2, 'Diamond Glass Marking Pencil');
Query OK, 1 row affected (0.03 sec)

mysql> insert into product(product_id, product_name) 
       values(3, 'Apsana Black Pencil');
Query OK, 1 row affected (0.03 sec)

mysql> insert into product_stock(product_id, qty, branch_id) 
       values(1, 100, 1);
Query OK, 1 row affected (0.03 sec)

mysql> insert into product_stock(product_id, qty, branch_id) 
       values(1, 50, 2);
Query OK, 1 row affected (0.03 sec)

mysql> insert into product_stock(product_id, qty, branch_id) 
       values(2, 80, 1);
Query OK, 1 row affected (0.03 sec)

my query:

mysql> SELECT IFNULL(SUM(s.qty),0) AS stock, 
              product_name 
       FROM product_stock s 
        RIGHT JOIN product p ON s.product_id=p.product_id
       WHERE branch_id=1 
       GROUP BY product_name 
       ORDER BY product_name;

returns:

+-------+-------------------------------+ 
| stock | product_name                  | 
+-------+-------------------------------+ 
| 100   | Apsana White DX Pencil        | 
|  80   | Diamond Glass Marking Pencil  | 
+-------+-------------------------------+ 
1 row in set (0.00 sec)

But I want to have the following result:

+-------+------------------------------+ 
| stock | product_name                 | 
+-------+------------------------------+ 
|   0   | Apsana Black Pencil          | 
| 100   | Apsana White DX Pencil       | 
|  80   | Diamond Glass Marking Pencil | 
+-------+------------------------------+

To get this result what mysql query should I run?

A: 

Your problem is with the where clause:

WHERE branch_id = 1

As the item with stock 0 doesn't have a product_stock record, there is also no branch_id, so there is no result row for that record.
Try your query without the where clause, and you should have 3 results.

fretje
A: 

Move the condition into the JOIN clause:

SELECT  IFNULL(SUM(s.qty),0) AS stock, 
        p.product_name 
FROM    product_stock s
RIGHT JOIN
        product p
ON      s.product_id = p.product_id
        AND branch_id=1 
GROUP BY
        p.product_id
ORDER BY
        p.product_name

or just rewrite it as a subquery:

SELECT  (
        SELECT  COALESCE(SUM(qty), 0)
        FROM    product_stock s
        WHERE   s.product_id = p.product_id
                AND s.branch_id = 0
        ) AS stock,
        product_name 
FROM    product p
ORDER BY
        p.product_name

WHERE is evaluated after the JOIN, that's why the missing products are filtered out (their branch_id is replaced with a NULL by the OUTER JOIN)

Quassnoi