SQL Left Join返回的内容少于预期

时间:2013-08-17 23:55:38

标签: mysql sql left-join

我有一个数据库,我需要将表PEDIDOC中的数据与FALTANTE混合在一起,但我需要进行左连接,因为即使没有匹配的FALTANTE,我也需要PEDIDOC中的所有数据。我有三重检查,所有信息都正确输入,一切都在那里,如果我只是做Select * FROM pedidoc我得到了我在Excel中过滤它时所期望的,但是当我做我的左连接时,那么就有产品丢失了。 / p>

这是查询

SELECT
        `pedidoc`.`fecha`,
        `pedidoc`.`IdPedido`, `pedidoc`.`plaza`, `pedidoc`.`IdProducto`,
        `pedidoc`.`Categoria`,`pedidoc`.`Pedido`,`faltante`.`faltante`
FROM `pedidoc`
        LEFT JOIN `caducidad` ON `pedidoc`.`IdProducto`=`faltante`.`IdProducto`
GROUP BY `pedidoc`.`fecha`, `pedidoc`.`IdProducto`

这是数据库

CREATE TABLE Faltante (
    IdProducto DECIMAL(17,0) NOT NULL,
    Plaza VARCHAR(40) NOT NULL,
    Fecha VARCHAR(10) NOT NULL,
    Faltante INT NULL,
    FOREIGN KEY(IdProducto) REFERENCES Producto(IdProducto),
    UNIQUE(IdProducto, Fecha, Plaza)
)Engine=InnoDB;

CREATE TABLE pedidoc (
    IdProducto DECIMAL(17,0) NOT NULL,
    IdPedido VARCHAR(40) NOT NULL,
    Plaza VARCHAR(40) NOT NULL,
    Fecha VARCHAR(10) NOT NULL,
    Categoria VARCHAR(40) NOT NULL,
    Pedido INT NULL,
    FOREIGN KEY(IdProducto) REFERENCES Producto(IdProducto),
    UNIQUE(IdProducto, Fecha, Plaza)
)Engine=InnoDB;

它确实返回的数据是正确的(采取了一些随机样本),但缺少了一半以上的pedidoc数据。

我的查询有什么问题?

仅供参考我在我的测试机器和WAMP堆栈上运行Windows 8。

Saludos, 古斯塔沃

编辑>这是我在“第一天”获得的表格。问题是每天只需要一件产品,每个广场每天只有一件产品。应该有一个产品清单,每个广场几乎相同,但对于蒙特雷它只显示2,而不是所有产品

fecha           IdPedido    plaza   IdProducto  Categoria   Pedido  caducidad
01/01/2012  2589970-20  Mexico  4111    Bigdonuts   0   0
01/01/2012  2589970-20  Mexico  7920    Bigdonuts   406 0
01/01/2012  2589970-20  Mexico  7921    Bigdonuts   425 0
01/01/2012  2589970-20  Mexico  7922    Bigdonuts   712 0
01/01/2012  2589970-20  Mexico  7923    Bigdonuts   454 0
01/01/2012  2589970-20  Mexico  7924    Bigdonuts   31  0
01/01/2012  2589970-20  Mexico  7925    Bigdonuts   11  0
01/01/2012  2589970-20  Mexico  7926    Bigdonuts   147 0
01/01/2012  2590100-10  Monterrey   7928    Bigdonuts   128 0
01/01/2012  2590100-10  Monterrey   7929    Bigdonuts   70  0
01/01/2012  2590090-30  Reynosa 7931    Big Donuts  12  0
01/01/2012  2590090-30  Reynosa 7932    Big Donuts  154 0
01/01/2012  2590090-30  Reynosa 7933    Big Donuts  23  0
01/01/2012  2590090-30  Reynosa 7934    Big Donuts  169 0
01/01/2012  2590090-30  Reynosa 7935    Big Donuts  50  0
01/01/2012  2589970-20  Mexico  7936    Bigdonuts   352 0
01/01/2012  2590100-10  Monterrey   7937    Bigdonuts   0   0
01/01/2012  2590090-30  Reynosa 7938    Big Donuts  296 0
01/01/2012  2590090-30  Reynosa 7939    Big Donuts  12  0
01/01/2012  2590080-50  Saltillo    7941    Bigdonuts   64  0
01/01/2012  2590080-50  Saltillo    7942    Bigdonuts   38  0
01/01/2012  2589970-20  Mexico  7944    Bigdonuts   269 0
01/01/2012  2589970-20  Mexico  7945    Bigdonuts   284 0
01/01/2012  2589970-20  Mexico  7946    Bigdonuts   320 0
01/01/2012  2589970-20  Mexico  7954    Bigdonuts   0   0
01/01/2012  2589970-20  Mexico  7969    Bigdonuts   334 0
01/01/2012  2589970-20  Mexico  7970    Bigdonuts   246 0
01/01/2012  2589970-20  Mexico  7971    Bigdonuts   39  0
01/01/2012  2589970-20  Mexico  7972    Bigdonuts   327 0
01/01/2012  2589970-20  Mexico  8071    Bigdonuts   0   0
01/01/2012  2590080-50  Saltillo    8112    Bigdonuts   0   0
01/01/2012  2590080-50  Saltillo    8113    Bigdonuts   0   0
01/01/2012  2590080-50  Saltillo    8114    Bigdonuts   0   0
01/01/2012  2590080-50  Saltillo    8115    Bigdonuts   0   0
01/01/2012  2590080-50  Saltillo    8116    Bigdonuts   0   0
01/01/2012  2590080-50  Saltillo    8117    Bigdonuts   0   0
01/01/2012  2589970-20  Mexico  8212    Bigdonuts   0   0
01/01/2012  2589970-20  Mexico  8453    Bigdonuts   0   0
01/01/2012  2589970-20  Mexico  8454    Bigdonuts   0   0
01/01/2012  2589970-20  Mexico  8456    Bigdonuts   0   0
01/01/2012  2589970-20  Mexico  8457    Bigdonuts   0   0
01/01/2012  2590100-10  Monterrey   68895   Bigdonuts   0   0

2 个答案:

答案 0 :(得分:0)

支持GROUP BY以减少行数

我认为你想要ORDER BY

如果您只想消除重复记录,可以使用SELECT DISTINCT。只需在SELECT关键字后添加DISTINCT关键字,如下所述:http://www.w3schools.com/sql/sql_distinct.asp

然而,我不确定这是否会给你你想要的东西。 ruakh说这是一个非便携式的扩展是正确的,对于你来说更重要的是,正如在ruakh的回答中提到的那样,GROUP BY将把所有记录的所有记录分成一个记录,其中每个记录的值都相同。 GROUP BY。

对于SELECT中提到的GROUP BY中未提及的任何字段,MySQL将随机选择这些记录中这些字段的任何值。如果所有这些记录对这些字段具有相同的值,则选择哪条记录无关紧要。例如,在上面的示例数据中,Categoria对于墨西哥广场的所有第一组八个记录都是Bigdonuts,因此选择Categoria的值无关紧要。

然而,对于Pedido,显示了几个不同的值,包括0,406和425.MySQL将随机选择这八个不同值中的一个。我无法想象这就是你想要的。

这就是为什么一般情况下,除非您知道在GROUP BY中分组的所有记录都具有相同的字段值,否则您可能希望使用其中一个聚合函数,如SUM,COUNT等。 :http://www.w3schools.com/sql/sql_functions.asp

答案 1 :(得分:0)

只需更改

GROUP BY `pedidoc`.`fecha`, `pedidoc`.`IdProducto`

GROUP BY pedidoc.IdProducto, pedidoc.fecha, pedidoc.Plaza

因此它与UNIQUE上的pedidoc约束相匹配,从而确保pedidoc的每一行都只出现一次。

(注意:无论是否有此更改,您的查询都无法移植到其他DBMS,因为它取决于特殊的MySQL行为;请参阅§12.17.3 "MySQL Extensions to GROUP BY" in the MySQL 5.6 Reference Manual。我假设是O.K。?)

相关问题