2014-12-06 238 views
2

此查询计算总和p_quantity <= 21000p_description。查询工作,但是当你添加ORDER BY p_reference显示所有行如何在MySQL中使用ORDER BY使用变量SET

我尝试添加ORDER BY这样的:

SET @runtot:=0; 
SELECT p_id, p_description, (@runtot := @runtot + p_quantity) AS runningTotal 
FROM product_table 
WHERE @runtot + p_quantity <= 21000 AND p_description = 'product_1' ORDER BY p_reference ASC 

这是创建产品表代码:

CREATE TABLE IF NOT EXISTS `product_table` (
    `p_id` int(11) PRIMARY KEY AUTO_INCREMENT NOT NULL, 
    `p_description` varchar(50) NOT NULL, 
    `p_reference` varchar(25) NOT NULL, 
    `p_location` varchar(25) NOT NULL, 
    `p_quantity` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

INSERT INTO `product_table` (`p_id`, `p_description`, `p_reference`, `p_location`, `p_quantity`) VALUES 
    (1, 'Product_1', '1A00001', 'AP07', 7000), 
    (2, 'Product_1', '1A00001', 'AF05', 6000), 
    (3, 'Product_1', '1A00233', 'DS07', 7000), 
    (4, 'Product_1', '1A00233', 'SD10', 5000), 
    (5, 'Product_1', '1A00001', 'YB12', 7000), 
    (6, 'Product_1', '1A00001', 'AN01', 7000), 
    (7, 'Product_1', '1A00001', 'AP04', 7000), 
    (8, 'Product_1', '1A00245', 'AP01', 7000), 
    (9, 'Product_1', '1A00001', 'QD01', 7000), 
    (10, 'Product_1', '1A00001', 'SC01', 7000); 
+0

我不明白这个问题。你想要点什么?它在我看来你在发布的查询中使用ORDER BY,那有什么问题? – Jaylen 2014-12-06 19:19:42

+0

当您添加ORDER BY p_reference显示所有行并且p_quantity> 21000时,但没有添加ORDER BY p_reference时,总p_quantity <= 21000 – phptraining 2014-12-06 20:00:53

+0

此问题比它看起来更难。事实上,我投票你的问题给我一个挑战:)请检查我的答案 – Jaylen 2014-12-06 21:54:28

回答

0

因为你没” t在查询中选择p_reference。尝试,

SET @runtot:=0; 
SELECT p_id, p_description, **p_reference**, (@runtot := @runtot + p_quantity) AS runningTotal 
FROM product_table 
WHERE @runtot + p_quantity <= 21000 AND p_description = 'product_1' 
ORDER BY p_reference ASC; 
+0

这不会有什么区别。这里的问题是什么时候订购记录 – Jaylen 2014-12-06 21:57:42

0

我尝试这样的查询,但显示的所有行和p_quantity > 21000

SELECT p_id, p_description, p_reference, (@runtot := @runtot + p_quantity) AS runningTotal 
FROM product_table 
WHERE @runtot + p_quantity <= 21000 AND p_description = 'product_1' 
ORDER BY p_reference ASC; 

这个查询而不ORDER BY显示p_quantity <=21000

SELECT p_id, p_description, p_reference, (@runtot := @runtot + p_quantity) AS runningTotal 
FROM product_table 
WHERE @runtot + p_quantity <= 21000 AND p_description = 'product_1'; 
+0

你应该在下面检查我的答案。 – Jaylen 2014-12-06 21:56:59

+1

这是一个答案或对你的问题的补充?如果这不是一个答案,你应该[编辑你的问题](http://stackoverflow.com/posts/27335294/edit)包含这些信息。只有实际的答案应该发布为答案。 – 2015-01-28 13:36:29

0

好吧,这个问题是不是更难了什么看起来像。事实上,我正在投票提出你的问题张贴这个问题:)

这里的问题是什么时候订购你的数据和什么时候应用你的数学。

想一想吧。您在p_quantity值加起来以前p_quantity

因此,如果我们有这样

Preference Quantity 
D   7000 
B   6000 
C   7000 
A   1500 

7000 is <= 2100 so Row D is selected 
7000 + 6000 = 13000 is <= than 21000 so Row B is selected 
13000 + 7000 = 20000 is <= 21000 so Row C is selected 
20000 + 1500 = 21500 is > 210000 Row A is NOT selected and no other rows will be selected 

列表的最后行会

Preference Quantity 
D   7000 
B   6000 
C   7000 

但是,如果我们通过分类列表Preference列先做,然后做相同的数学

Preference Quantity 
A   1500 
B   6000 
C   7000 
D   7000 

1500 is <= 21000 so row A is selected 
1500 + 6000 = 7500 is <= 21000 row B is selected 
7500 + 7000 = 14500 is <= 21000 row C is selected 
14500 + 7000 = 21500 is > 21000 row D is not selected and nothing after will be selected 

最终的名单将

Preference Quantity 
A   1500 
B   6000 
C   7000 

这里的想法是,最终的结果集是怎么回事,当你做你的数学要根据各行的位置不同。

它得到解决你的问题,首先要对列表进行排序,然后运行你的数学:)

下面的查询应该解决您的问题,给你,你正在寻找

SET @runtot=0; 
-- second go through the sorted set and run your math 
SELECT p_reference, p_id, p_description, (@runtot := @runtot + p_quantity) AS runningTotal 
FROM (
    -- First generate a sorted data set 
    SELECT p_reference, p_id, p_description, p_quantity 
    FROM product_table 
    WHERE p_description = 'product_1' AND p_quantity <= 21000 
    ORDER BY p_reference 
) AS l 
WHERE @runtot + p_quantity <= 21000; 
数据集