简体   繁体   中英

Query returns too few rows

setup:

mysql> create table product_stock(
       product_id integer, qty 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) 
       values(1, 100);
Query OK, 1 row affected (0.03 sec)

my first query:

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

returns:

+-------+---------------------------+ 
| stock | product_name              | 
+-------+---------------------------+ 
| 100   | Apsana White DX 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       | 
|   0   | Diamond Glass Marking Pencil | 
+-------+------------------------------+ 

To get this result what mysql query should I run?

An INNER join will only return rows that have a match in both tables. Which is why results for which there are no row in the stock table returns no results.

A LEFT join will return all rows in the first table, and a RIGHT join will return all rows in the second table.

In your query you are expecting all results from the second table, so change your INNER join to a RIGHT join.

There is a tutorial here, with some examples:

http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html

Do an outer join from product to product_stock , not an inner join from product_stock to product .

(Good work on making the question clear, complete, and unambiguous.)

You need to flip your join around and use LEFT JOIN instead of INNER JOIN:

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

如果我已正确阅读您的问题,则只需将您的INNER JOIN更改为RIGHT OUTER JOIN

If you are using the stock table as a base, then you'll only get one item, since it only has 1 point of reference to the other table.

Use the product table and join into the stock table. You'll probably get NULL as a stock value but you can handle that with server side code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM