未知栏' CURRENT_STOCK_LEVEL'在'字段列表'
$queryString = 'SELECT A.ITEM_CODE, A.BEGINNING_BALANCE AS BEGINNING_BALANCE, B.DELIVERY AS DELIVERY, C.ISSUANCE AS ISSUANCE,
(IFNULL(BEGINNING_BALANCE, 0) + IFNULL(DELIVERY, 0)) - IFNULL(ISSUANCE, 0) AS CURRENT_STOCK_LEVEL , A.REORDERPNT AS REORDERPNT,
IF( CURRENT_STOCK_LEVEL <= REORDERPNT, "LOW", "HIGH") AS STATUS
FROM MM_NEW_ROP_ITEMS AS A
LEFT OUTER JOIN (SELECT ITEM_CODE, SUM(QUANTITY) AS ISSUANCE FROM MM_NEW_ROP_ISSUANCES GROUP BY ITEM_CODE) AS C ON A.ITEM_CODE = C.ITEM_CODE
LEFT OUTER JOIN (SELECT ITEM_CODE, SUM(QUANTITY) AS DELIVERY FROM MM_NEW_ROP_DELIVERIES GROUP BY ITEM_CODE) AS B ON A.ITEM_CODE = B.ITEM_CODE
ORDER BY A.ITEM_CODE';
$query = mysql_query($queryString) or die(mysql_error());
//makes a loop and creates an array with query fields
$items = array();
while($item = mysql_fetch_assoc($query)) {
$items[] = $item;
}
//encodes for JSON format
echo json_encode(array(
"success" => mysql_errno() == 0,
"items" => $items
));
答案 0 :(得分:0)
这是您的SELECT
:
SELECT A.ITEM_CODE, A.BEGINNING_BALANCE, B.DELIVERY AS DELIVERY, C.ISSUANCE,
(IFNULL(BEGINNING_BALANCE, 0) + IFNULL(DELIVERY, 0)) - IFNULL(ISSUANCE, 0) AS CURRENT_STOCK_LEVEL ,
A.REORDERPNT AS REORDERPNT,
IF(CURRENT_STOCK_LEVEL <= REORDERPNT, 'LOW', 'HIGH') AS STATUS
如错误所示,您无法在定义它的同一SELECT
语句中重用列别名。这不仅适用于SELECT
,也适用于查询的其他组成部分,例如WHERE
。
所以,重复逻辑,我更喜欢COALESCE()
和CASE
(这是ANSI标准函数):
SELECT A.ITEM_CODE, A.BEGINNING_BALANCE, B.DELIVERY AS DELIVERY, C.ISSUANCE,
(COALESCE(BEGINNING_BALANCE, 0) + COALESCE(DELIVERY, 0)) - COALESCE(ISSUANCE, 0)) AS CURRENT_STOCK_LEVEL ,
A.REORDERPNT AS REORDERPNT,
(CASE WHEN COALESCE(BEGINNING_BALANCE, 0) + COALESCE(DELIVERY, 0)) -
COALESCE(ISSUANCE, 0) <= REORDERPNT
THEN 'LOW' ELSE 'HIGH'
END) AS STATUS
答案 1 :(得分:0)
您可以使用MySQL会话变量来完成您的工作,而无需重复逻辑。
以下是在查询中执行此操作的方法:
SELECT A.ITEM_CODE, A.BEGINNING_BALANCE AS BEGINNING_BALANCE, B.DELIVERY AS DELIVERY, C.ISSUANCE AS ISSUANCE,
@temp_stock_level:=0, @temp_stock_level:=(IFNULL(BEGINNING_BALANCE, 0) + IFNULL(DELIVERY, 0)) - IFNULL(ISSUANCE, 0) AS CURRENT_STOCK_LEVEL ,
A.REORDERPNT AS REORDERPNT,IF( @temp_stock_level <= REORDERPNT, "LOW", "HIGH") AS STATUS
FROM MM_NEW_ROP_ITEMS AS A
LEFT OUTER JOIN (SELECT ITEM_CODE, SUM(QUANTITY) AS ISSUANCE FROM MM_NEW_ROP_ISSUANCES GROUP BY ITEM_CODE) AS C ON A.ITEM_CODE = C.ITEM_CODE
LEFT OUTER JOIN (SELECT ITEM_CODE, SUM(QUANTITY) AS DELIVERY FROM MM_NEW_ROP_DELIVERIES GROUP BY ITEM_CODE) AS B ON A.ITEM_CODE = B.ITEM_CODE
ORDER BY A.ITEM_CODE;
在此查询中,我使用了@temp_stock_level变量来存储值CURRENT_STOCK_LEVEL