基于列的SUM限制结果

时间:2014-12-01 07:44:01

标签: mysql

如何选择数量记录范围。

tblStock
+----------+------------+----------+----------+------------+
+ Stock ID + Product ID + Quantity + Amount   + Date       +
+----------+------------+----------+----------+------------+
+        1 +       1001 +       50 +    10.00 + 2014-11-10 +
+----------+------------+----------+----------+------------+
+        2 +       1001 +       70 +    11.00 + 2014-11-11 +
+----------+------------+----------+----------+------------+
+        3 +       1001 +       30 +     9.90 + 2014-11-12 +
+----------+------------+----------+----------+------------+
+        4 +       1001 +       10 +    10.20 + 2014-11-13 +
+----------+------------+----------+----------+------------+

示例我想获得前100个数量记录。 (结果A)

Result A
+----------+------------+----------+----------+------------+
+ Stock ID + Product ID + Quantity + Amount   + Date       +
+----------+------------+----------+----------+------------+
+        1 +       1001 +       50 +    10.00 + 2014-11-10 +
+----------+------------+----------+----------+------------+
+        2 +       1001 +       50 +    11.00 + 2014-11-11 +
+----------+------------+----------+----------+------------+

示例我想获得最后60个数量记录。 (结果B)

Result B
+----------+------------+----------+----------+------------+
+ Stock ID + Product ID + Quantity + Amount   + Date       +
+----------+------------+----------+----------+------------+
+        2 +       1001 +       20 +    11.00 + 2014-11-11 +
+----------+------------+----------+----------+------------+
+        3 +       1001 +       30 +     9.90 + 2014-11-12 +
+----------+------------+----------+----------+------------+
+        4 +       1001 +       10 +    10.20 + 2014-11-13 +
+----------+------------+----------+----------+------------+

1 个答案:

答案 0 :(得分:0)

只需相应地更改ORDER BY子句中的ASC和DESC。

结果A:

SELECT * FROM table_name
ORDER BY StockID DESC
LIMIT 100

结果B:

SELECT * FROM table_name
ORDER BY StockID ASC
LIMIT 60

谢谢。