mysql有语句返回没有行

时间:2013-06-25 00:25:34

标签: mysql

我正在尝试根据fifo_costfifo_in_date之间的差异将正确的order_date分配给给定订单:fifo_costorder_date之间的最小差异相关联{1}}和fifo_date_in应该分配给该订单。

以下mysql代码段不会返回任何记录。我希望它能够返回fifo_date_in最接近order_date的那条记录,但显然我错过了一些东西。

drop table if exists tmp;

create table tmp (
order_sequence int,
order_number int,
order_date date,
fifo_date_in date,
fifo_cost float);

INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-01-01',1.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-02-01',2.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-03-01',3.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-04-01',4.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-05-01',5.55);

SELECT
  order_sequence, order_number, order_date, fifo_date_in, fifo_cost, datediff(order_date,fifo_date_in) as ddiff
FROM tmp
GROUP BY order_sequence, order_number, order_date
HAVING datediff(order_date,fifo_date_in) = min(datediff(order_date,fifo_date_in))

3 个答案:

答案 0 :(得分:3)

如果你想得到这笔费用,我想你必须找到min并加入回到基表:

SELECT t.order_sequence, t.order_number, t.order_date, t.fifo_date_in, t.fifo_cost
  FROM tmp t
  INNER JOIN ( SELECT order_sequence, order_number, order_date
                     ,MIN(datediff(order_date,fifo_date_in)) as ddiff
                 FROM tmp
                 GROUP BY order_sequence, order_number, order_date
              ) m
         ON (m.order_sequence = t.order_sequence
             AND m.order_number = t.order_number
             AND m.order_date = t.order_date
             AND datediff(t.order_date, t.fifo_date_in) = m.ddiff)

此外,如果最接近可能意味着在之前或之后,您可能必须考虑绝对值。

这是SQLFiddle

答案 1 :(得分:0)

您的查询存在问题。

您要做的是选择中的MIN(datediff(order_date,fifo_date_in)) as ddiff,它会找到差异的最低值。然后,您可以使用group by对结果进行分组。从以前的评论中抓取我的评论,你的group by实际上是正确的:

drop table if exists tmp;

create table tmp (
order_sequence int,
order_number int,
order_date date,
fifo_date_in date,
fifo_cost float);

INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-01-01',1.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-02-01',2.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-03-01',3.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-04-01',4.55);
INSERT INTO tmp (order_sequence, order_number, order_date, fifo_date_in, fifo_cost) VALUES (5613, 561, '2013-01-02','2009-05-01',5.55);


SELECT
  order_sequence, order_number, order_date, fifo_date_in, fifo_cost, MIN(datediff(order_date,fifo_date_in)) as ddiff
FROM tmp
GROUP BY order_sequence, order_number, order_date

答案 2 :(得分:0)

您可以使用ORDER BY和LIMIT,并省略GROUP BY:

SELECT
  order_sequence, order_number, order_date, fifo_date_in, fifo_cost, datediff(order_date,fifo_date_in) as ddiff
FROM tmp 
ORDER BY ddiff
LIMIT 1