Mysql

如何對另一個查詢結果進行查詢操作

  • December 26, 2019

如何使用 sql 查詢從以下結果中獲得批准未批准已刪除的計數。

SELECT approved,deleted_at,count(id)AS count FROM `payment_voucher` 
   WHERE company_id = 1 GROUP BY approved,deleted_at;

我將上述查詢結果作為圖像附加。

在此處輸入圖像描述

所需輸出:已批准:32 not_approved:4已刪除:7

SELECT SUM(CASE WHEN (approved=1) AND (deleted_at IS NULL) THEN `count` END) approved,
      SUM(CASE WHEN (approved=0) AND (deleted_at IS NULL) THEN `count` END) not_approved,
      SUM(CASE WHEN deleted_at IS NOT NULL THEN `count` END) deleted
FROM (your subquery) source

引用自:https://dba.stackexchange.com/questions/256305