我需要有关如何使用MySQL创建货件装箱单的帮助。
假设我有32盒键盘可以发货,主纸箱可以包含12个盒子。
我只有32个盒子的值和12个体积。下面结果中的另一个值是由sql命令生成的。不是来自唱片。
因此,这很容易计算出主纸箱的数量将是3个主纸箱,其中一个作为非标准数量。如何对此进行查询?
我想成为这样的结果:
+----------+---------------+-------------------+--------+------------+---------+
| Quantity | Standard_Qty | Non_Standard_Qty | Box_N | Box_Total | RowType |
+----------+---------------+-------------------+--------+------------+---------+
| 12 | 1 | 0 | 1 | 3 | Detail |
| 12 | 1 | 0 | 2 | 3 | Detail |
| 8 | 0 | 1 | 3 | 3 | Detail |
| 32 | 2 | 1 | | | Summary |
+----------+---------------+-------------------+--------+------------+---------+
它看起来像我知道的两个查询,可能使用了FLOOR命令,其中我在here教授。如何取得这个结果?
提前致谢。 斯蒂芬
谢谢Kai,
但是我想通过SQL命令生成它,而不是通过客户端编程生成它。 我就像这样堆在中间。无论如何,这不是一个完整的专栏, 我打算在我的原帖中。
mysql> SELECT
-> art_name,
-> color_code,
-> volume,
-> SUM(quantity) AS total,
-> FLOOR(SUM(quantity) / volume) as boxes_sq,
-> (SUM(quantity) % volume) as box_nsq_contain
-> FROM order_main
-> WHERE order_main_id = "11"
+----------+------------+--------+-------+----------+-----------------+
| art_name | color_code | volume | total | boxes_sq | box_nsq_contain |
+----------+------------+--------+-------+----------+-----------------+
| KEYBOA | CAR | 12 | 5 | 0 | 5 |
| KEYBOA | CAR | 12 | 9 | 0 | 9 |
| KEYBOA | CAR | 12 | 15 | 1 | 3 |
| KEYBOA | CAR | 12 | 20 | 1 | 8 |
| KEYBOA | CAR | 12 | 12 | 1 | 0 |
| KEYBOA | CAR | 12 | 6 | 0 | 6 |
| KEYBOA | CAR | 12 | 3 | 0 | 3 |
| KEYBOA | CSM | 12 | 5 | 0 | 5 |
| KEYBOA | CSM | 12 | 9 | 0 | 9 |
| KEYBOA | CSM | 12 | 17 | 1 | 5 |
| KEYBOA | CSM | 12 | 21 | 1 | 9 |
| KEYBOA | CSM | 12 | 14 | 1 | 2 |
| KEYBOA | CSM | 12 | 6 | 0 | 6 |
| KEYBOA | CSM | 12 | 3 | 0 | 3 |
+----------+------------+--------+-------+----------+-----------------+
答案 0 :(得分:0)
SQL不像处理数据那样进行优化,而是检索和存储数据。出于这个原因,你得到的任何答案都会相对复杂(我想)。更简单的方法是使用您正在执行SQL查询的任何语言来处理它。伪代码可能类似于:
int num_keyboards = SELECT COUNT(*) FROM keyboards
int num_standard_packages = num_keyboards / 12
int keyboards_in_non_standard_package = num_keyboards % 12 (if 0 then discard)