1

我正在尝试从我的 MySQL 表中获取一些统计信息,并希望将其保留为单个查询。只是不知道如何(或者甚至可能)。我有这两个查询:

SELECT COUNT(*) AS `accept` 
FROM `status` 
WHERE `groupID` IN (98779,98780) 
  AND `group` = 'order' 
  AND `status` = 'accept'

SELECT COUNT(*) AS `price` 
FROM `status` 
WHERE `groupID` IN (98779, 98780) 
  AND `group` = 'quotation' 
  AND (`status` = 'final' OR `status` = 'manualprice')

我最好的建议是将它们组合成一个SELECT这样的:

SELECT
    (SELECT COUNT(*) AS `accept` 
     FROM `status`
     WHERE `groupID` IN (98779, 98780) AND `group` = 'order' AND `status` = 'accept') AS accept,
    (SELECT COUNT(*) AS `price` 
     FROM `status` 
     WHERE `groupID` IN (98779, 98780) AND `group` = 'quotation' AND (`status` = 'final' OR `status` = 'manualprice')) AS price

有没有更好的办法?

4

2 回答 2

1

这是另一种方法,

SELECT  SUM(`group` = 'order' AND `status` = 'accept') AS `ACCEPT`,
        SUM(`group` = 'quotation' AND (`status` = 'final' OR `status` = 'manualprice')) AS `PRICE`
FROM    `status`
WHERE   `groupID` IN (98779, 98780) AND
        `group` IN ('order', 'quotation') AND
        `status` IN ('accept', 'final', 'manualprice')
于 2013-08-16T12:06:03.510 回答
0
SELECT 
    sum(if(`group` = 'order' AND `status` = 'accept', 1,0)) as `accept`,
    sum(if(`group` = 'quotation' AND (`status` = 'final' OR `status` = 'manualprice'), 1,0)) as `price`
FROM 
    `status` 
WHERE 
    `groupID` IN (98779,98780) 
    AND `group` IN ('order', 'quotation') 
    AND `status` IN ('accept', 'final', 'manualprice')

编辑这与 491243 的其他答案基本相同。我使用了额外的 if 语句,似乎不一定需要。

于 2013-08-16T12:09:44.123 回答