“字段列表”中的未知列“ 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
));
最佳答案
这是您的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
关于php - 我如何在if语句中使用mysql别名列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32020845/