【发布时间】:2013-08-17 23:55:38
【问题描述】:
我有一个数据库,我需要获取表 PEDIDOC 上与 FALTANTE 混合的数据,但我需要进行左连接,因为即使没有匹配的 FALTANTE,我也需要 PEDIDOC 中的所有数据。我进行了三次检查,所有信息都正确输入,一切都在那里,如果我只是做 Select * FROM pedidoc,我在 Excel 中过滤它时得到了我期望的结果,但是当我做左连接时,就会缺少产品。
这里是查询
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。
大家好, 古斯塔沃
EDIT> 这是我在“第一”天得到的表格。问题是它每天只需要一种产品,而实际上每个广场每天都有一种产品。应该有一个产品列表,每个广场几乎相同,但对于蒙特雷来说只显示 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
【问题讨论】:
-
您能否将数据发布到缺少的几条记录中?
-
顺便说一句,在您的查询中,您将 pedidoc 表与 caducidad 表连接,而不是 Faltante。
-
我认为问题中的 SQL 语句实际上不会执行,因为
select中的某些字段不在group by或聚合语句的一部分中。 -
很抱歉,我把语句放在这里时打错了,但它确实执行了,我将把它给出的第 1 天的表格粘贴到主帖上