简体   繁体   English

查询返回的行太少

[英]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? 为了得到这个结果,我应该运行什么mysql查询?

An INNER join will only return rows that have a match in both tables. INNER联接将仅返回两个表中都具有匹配项的行。 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. 在查询中,您期望第二个表中的所有结果,因此将您的INNER联接更改为RIGHT联接。

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 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 . productproduct_stock进行外部product_stock ,而不是从product_stockproduct进行内部product_stock

(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: 您需要翻转联接并使用LEFT JOIN代替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. 您可能会得到NULL作为库存值,但是您可以使用服务器端代码来处理该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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