我有来自不同请求的结果作为例子。
第一次要求:
SELECT SUM(PRODUCT_QTY) FROM TABLEBLA
WHERE STATE='DONE' and location_dest_id in (12,18) and location_id=5
给出:
sum(product_qty)
----------------
1200
第二次要求:
SELECT SUM(PRODUCT_QTY) FROM TABLEBLA
WHERE STATE='CANCEL' and location_id (12,18) and location_dest_id=5
给出:
sum(product_qty)
----------------
900
我希望将结果显示为1200-900 = 300
答案 0 :(得分:2)
一种简单的方法是条件聚合:
WHERE
或者,如果这是较大查询的一部分,并且您不希望在SELECT (SUM(CASE WHEN STATE = 'DONE' THEN PRODUCT_QTY ELSE 0 END) -
SUM(CASE WHEN STATE = 'CANCEL' THEN PRODUCT_QTY END)
) as diff
FROM TABLEBLA
WHERE location_dest_id in (12, 18) and location_id = 5;
子句中进行过滤:
$print_projects .= <<<HTML
<div id="$id" class="project">
$projectname has the number $projectnumber
<div class="specific-informations">
Projectname: $projectname<br>
Projectnumber: $projectnumber
</div>
</div>
HTML;
答案 1 :(得分:1)
SELECT SUM(CASE WHEN STATE = 'DONE' THEN PRODUCT_QTY ELSE 0 END) -
SUM(CASE WHEN STATE = 'CANCEL' THEN PRODUCT_QTY ELSE 0 END)
FROM TABLEBLA WHERE location_id IN (12,18) AND location_dest_id=5