我有一个INV_MASTER表,用于存储每天清单中的项目列表,以及一个ITEM_MASTER表,其中包含项目列表及其详细信息(例如,部门,子部门,班级,子类等)。 )。
我正在尝试编写一个查询来从日期范围中提取清单中的项目列表(INV_MASTER),根据项目类别(ITEM_MASTER)按升序排列。这是我的查询它的抛出异常。
SELECT
INV_MASTER.LOC_CODE,
INV_MASTER.INV_DATE,
INV_MASTER.ITEM_CODE,
INV_MASTER.ITEM_DESC,
INV_MASTER.UNIT_DESC,
FIRST(INV_MASTER.BEG_QTY),
SUM(INV_MASTER.SOLD_QTY),
SUM(INV_MASTER.REC_QTY),
SUM(INV_MASTER.RET_QTY),
SUM(INV_MASTER.ADJ_QTY),
SUM(INV_MASTER.COUNT_P),
SUM(INV_MASTER.COUNT_C),
SUM(INV_MASTER.TRANS_IN),
SUM(INV_MASTER.TRANS_OUT),
LAST(INV_MASTER.END_QTY),
ITEM_MASTER.*
FROM INV_MASTER
LEFT OUTER JOIN ITEM_MASTER ON INV_MASTER.ITEM_CODE=ITEM_MASTER.ITEM_CODE
WHERE ITEM_MASTER.DEPARTMENT = '$department' AND ITEM_MASTER.SUB_DEPARTMENT = '$subdepartment' AND ITEM_MASTER.CLASS = '$class' AND ITEM_MASTER.SUB_CLASS = '$subclass' AND INV_MASTER.INV_DATE BETWEEN '$from' AND '$to'
GROUP BY INV_MASTER.LOC_CODE, INV_MASTER.ITEM_CODE
ORDER BY ITEM_MASTER.$type_desc, INV_MASTER.INV_DATE, ITEM_MASTER.ITEM_CODE
这是我的打印代码:
while($row = ibase_fetch_assoc($sql))
{
$inv_date = $row['INV_DATE'];
$item_code = $row['ITEM_CODE'];
$item_desc = $row['ITEM_DESC'];
$unit_desc = $row['UNIT_DESC'];
$sold_qty = $row['SUM(SOLD_QTY)'];
$beg_qty = $row['FIRST(BEG_QTY)'];
$rec_qty = $row['SUM(REC_QTY)'];
$ret_qty = $row['SUM(RET_QTY)'];
$adj_qty = $row['SUM(ADJ_QTY)'];
$count_p_qty = $row['SUM(COUNT_P)'];
$count_c_qty = $row['SUM(COUNT_C)'];
$trans_in_qty = $row['SUM(TRANS_IN)'];
$trans_out_qty = $row['SUM(TRANS_OUT)'];
$end_qty = $row['LAST(END_QTY)'];
*printing part here*
}
答案 0 :(得分:4)
错误消息说明:SELECT
子句中的所有列必须是聚合函数(例如SUM
,FIRST
)或GROUP BY
子句。
你有
INV_MASTER.LOC_CODE, INV_MASTER.ITEM_CODE
GROUP BY
子句中的,但是你选择了
INV_MASTER.LOC_CODE,
INV_MASTER.INV_DATE,
INV_MASTER.ITEM_CODE,
INV_MASTER.ITEM_DESC,
INV_MASTER.UNIT_DESC,
ITEM_MASTER.*
没有任何聚合功能。任
SELECT
子句或GROUP BY
子句
据我所知,您的PHP代码只需要非聚合列
INV_DATE, ITEM_CODE, ITEM_DESC, UNIT_DESC
SELECT
部分中的。因此,请删除其余部分,并确保GROUP BY
包含所有这四列。
编辑:基于SqlFiddle输入
SQL Fiddle数据并不是真正完整的,并且请求看起来与第一个问题(WHERE子句中有更多项目的地方)非常不同,但我设法获得了一个运行语句。
SELECT
ITEM_MASTER.ITEM_CODE,
ITEM_MASTER.ITEM_DESC,
ITEM_MASTER.UNIT_DESC,
ITEM_MASTER.CLASS,
MIN(INV_MASTER.BEG_QTY),
SUM(INV_MASTER.SOLD_QTY),
SUM(INV_MASTER.REC_QTY),
SUM(INV_MASTER.RET_QTY),
SUM(INV_MASTER.ADJ_QTY),
SUM(INV_MASTER.COUNT_P),
SUM(INV_MASTER.COUNT_C),
SUM(INV_MASTER.TRANS_IN),
SUM(INV_MASTER.TRANS_OUT),
MAX(INV_MASTER.END_QTY)
FROM INV_MASTER
LEFT OUTER JOIN ITEM_MASTER ON INV_MASTER.ITEM_CODE=ITEM_MASTER.ITEM_CODE
WHERE INV_MASTER.INV_DATE BETWEEN '2015-10-27' AND '2015-10-31'
GROUP BY ITEM_MASTER.ITEM_CODE, ITEM_MASTER.ITEM_DESC, ITEM_MASTER.UNIT_DESC, ITEM_MASTER.CLASS
ORDER BY ITEM_MASTER.ITEM_CODE, INV_MASTER.INV_DATE, ITEM_MASTER.ITEM_CODE
我希望这或多或少是你想要的,我认为无论如何都很容易适应。由于您在SQLFiddle中插入的数据不包含所有字段,因此我无法获得您想要的相同结果。
请记住,MySQL确实不知道函数FIRST
和LAST
,因此我将它们更改为MIN和MAX,不确定,如果这样做'好吧,不过。
答案 1 :(得分:0)
从这里的表格和示例数据http://www.sqlfiddle.com/#!9/b823f,当我查询从'2015-10-27'
到'2015-10-31'
的日期时,输出应该如下所示。它也应该根据其项目部门(例如金属,绘画......)进行分组。
ITEM_CODE | ITEM_DESC | UNIT_DESC | CLASS | BEG | + | - | - | - | + | + | + | + | - | - | END
METAL
000000000MS016 | MS RHS 50 X 150 X 3MM X 6M | LENGTH | NONE | 12.5 |20 |25 | 0 | 0 | 0 | 0 | 0 |30 | 0 | 5 | 32.5
PAINTING
000000000PN044 | DURAPATCH BODYFILLER W/ CREAM HARDENER | GALLON/S | NONE | 0 |70 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 65
NONE
000000000VI064 | TURQOUISE STICKER 3630-236 | SQR FT | NONE | 440 | 5 | 0 |10 | 0 |10 | 0 | 0 | 0 | 8 | 0 | 437
ART
000000000VI029 | 3M PLUM PURPLE 3630-128 | SQR FT | NONE | 274 |35 |10 | 0 |25 | 0 | 0 | 6 | 0 | 3 | 4 | 273