1

我试图弄清楚如何将两个和可能额外的选择查询放入一个视图的 1 个大查询中,但遇到了麻烦。最终结果应该是 1 行,其中包含两列的选定字段的计数。我是否为此使用子选择?

这是前两个查询:

SELECT
  COUNT(PIN.APP_UID) AS `Type A outstanding`
FROM PMT_INSP_NORMAL PIN
  WHERE
  PIN.APP_STATUS = "To_Do"
  AND
  PIN.DATE_COMPLETED IS NULL


SELECT
  COUNT(PSN.APP_UID) AS `Type B outstanding`
FROM PMT_SIGN_NORMAL PSN
  WHERE
  PSN.APP_STATUS = "To_Do"
  AND
  PSN.DATE_COMPLETED IS NULL
4

2 回答 2

1

尝试这个:

Select 
    (SELECT COUNT(PIN.APP_UID)
FROM PMT_INSP_NORMAL PIN
WHERE PIN.APP_STATUS = "To_Do"
AND PIN.DATE_COMPLETED IS NULL) as `Type A outstanding`,


(SELECT COUNT(PSN.APP_UID)
FROM PMT_SIGN_NORMAL PSN
WHERE
PSN.APP_STATUS = "To_Do"
AND PSN.DATE_COMPLETED IS NULL) AS `Type B outstanding`
于 2013-08-27T19:42:27.373 回答
0

是的,子查询可以做到这一点。它看起来像下面这样

SELECT (SELECT  COUNT(PIN.APP_UID) 
        FROM PMT_INSP_NORMAL PIN
         WHERE PIN.APP_STATUS = 'To_Do'  AND PIN.DATE_COMPLETED IS NULL
        ) AS 'Type A outstanding'
     ,
     (SELECT COUNT(PSN.APP_UID) 
        FROM PMT_SIGN_NORMAL PSN
        WHERE PSN.APP_STATUS = 'To_Do' AND PSN.DATE_COMPLETED IS NULL
) AS 'Type B outstanding'
于 2013-08-27T19:44:42.417 回答