[英]How to merge 2 queries which check for same flag columns but different value
I have 2 query which i used to fetch sum of values based on flag value 我有2个查询,我过去曾根据标志值获取值的总和
SELECT SUM(stock_deliveries.stockValue) AS stockValues1
FROM `stock_deliveries`
LEFT JOIN `delivery` ON `delivery`.`pk_deliveryId` = `stock_deliveries`.`fk_deliveryId`
WHERE `stock_deliveries`.`fk_stockID` = '189'
AND `delivery`.`delivery_completed` = '1'
SELECT SUM(stock_deliveries.stockValue) AS stockValues2
FROM `stock_deliveries`
LEFT JOIN `delivery` ON `delivery`.`pk_deliveryId` = `stock_deliveries`.`fk_deliveryId`
WHERE `stock_deliveries`.`fk_stockID` = '189'
AND `delivery`.`delivery_completed` = '0
Is it possible to merge this 2 query in single query ? 是否可以在单个查询中合并这2个查询? thanks in advance.
提前致谢。
Yes, you'd use conditional aggregation (ie CASE WHEN
inside an aggregation function like SUM
): 是的,您将使用条件聚合(即在
SUM
类的聚合函数中使用CASE WHEN
):
select
sum(case when d.delivery_completed = 0 then sd.stockvalue end) as stockvalues0,
sum(case when d.delivery_completed = 1 then sd.stockvalue end) as stockvalues1
from stock_deliveries sd
left join delivery d on d.pk_deliveryid = sd.fk_deliveryid
where sd.fk_stockid = 189;
You can use a single query with the help of GROUP BY 您可以在GROUP BY的帮助下使用单个查询
SELECT SUM(stock_deliveries.stockValue) AS stockValues, `delivery`.`delivery_completed` as deliveryCompleted
FROM `stock_deliveries`
LEFT JOIN `delivery` ON `delivery`.`pk_deliveryId` = `stock_deliveries`.`fk_deliveryId`
WHERE `stock_deliveries`.`fk_stockID` = '189'
GROUP BY `delivery`.`delivery_completed`;
Doing so you will have a row for every value of delivery_completed
with the associated sum (if you need only 0 and 1 values you can add to the where clause and delivery.delivery_completed IN (0,1)
). 这样做,您将为
delivery_completed
每个值及其相关的总和有一行(如果您只需要0和1的值,则可以添加到where子句and delivery.delivery_completed IN (0,1)
)。 Example: 例:
stockValues deliveryCompleted
1000 0
700 1
you can use "if" inside sum to check if you need the value in stockValues1
or stockValues0
您可以在总和内使用“ if”来检查是否需要
stockValues1
或stockValues0
的值
SELECT SUM(IF(d.delivery_completed = 1, sd.stockValue, 0)) AS stockValues1, SUM(IF(d.delivery_completed = 0, sd.stockValue, 0)) AS stockValues2
FROM stock_deliveries sd
LEFT JOIN delivery d ON d.pk_deliveryId = sd.fk_deliveryId
WHERE sd.fk_stockID = '189'
AND (d.delivery_completed = 1 OR d.delivery_completed = 0)
obviously if "delivery_completed" is only 1 or 0, you can omit the last AND
in the query ;) 显然,如果“ delivery_completed”仅为1或0,则可以省略查询中的最后一个
AND
;)
Edit: as Thorsten Kettner answered you can also use case-when syntax, with only one "case" they are substantially equivalent. 编辑:正如Thorsten Kettner回答的那样,您还可以使用case-when语法,只有一个“ case”是基本等同的。 The
if
syntax in this case is more concise, but if you have more options is more flexible, for instance: 这种情况下的
if
语法更简洁,但是如果您有更多选择,则更加灵活,例如:
SELECT CASE sometable
WHEN 'top' THEN 'high'
WHEN 'center' THEN 'medium'
ELSE 'low'
END AS position
您可以使用UNION ALL
运算符保留2个结果行的逻辑,但现在它们处于相同结果中,而不是2个分隔行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.