0

我想同时查询多个选择,我的查询在没有最后一个 SELECT 的情况下工作,但是我有一个错误,Operand should contain 1 column 有人可以告诉我如何更正吗?


SELECT (
 SELECT COUNT(*) FROM mytable WHERE TYPE = 'MSS' AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) 
) as TOTAL_MSS,
( 
SELECT COUNT(*) FROM mytable WHERE TYPE = 'MSS' AND S = 0 AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE())
) as MSS_S,
(
 SELECT COUNT(*) FROM mytable WHERE TYPE = 'MSS' AND S != 0 AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE())
) as MSS_F,
(
SELECT COUNT(*) as Nb ,OR as TOP FROM mytable WHERE TYPE = 'MSS'  AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) GROUP BY OR ORDER BY Nb DESC LIMIT 1
)

如果我分别执行它们,我会得到

TOTAL_MSS   MSS_S   MSS_F
99          12      87

Nb  TOP
78  j.r@domain.com
4

3 回答 3

2

试试这个查询

SELECT 
   * 
FROM 
(SELECT 
   1 as rId,
   count(*) as TOTAL_MSS,
   sum(if(S = 0, 1, 0)) as MSS_S,
   sum(if(S <> 0, 1, 0)) as MSS_F
FROM 
   mytable
WHERE
   TYPE = 'MSS' AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE())) a
INNER JOIN
   (SELECT 
       1 as rId,
       COUNT(*) as Nb, 
       OR as TOP 
   FROM 
       mytable 
   WHERE 
       TYPE = 'MSS'  AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) 
   GROUP BY 
       OR 
   ORDER BY 
       Nb DESC LIMIT 1)b
 ON 
   a.rID = b.rID;

希望这可以帮助...

于 2013-05-14T07:54:33.000 回答
1

像使用UNION

SELECT_STMT1 UNION SELECT_STMT2 UNION SELECT_STMT3 UNION SELECT_STMT4

作为

 SELECT COUNT(*) FROM mytable WHERE TYPE = 'MSS' AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) 
) as TOTAL_MSS UNION
( 
SELECT COUNT(*) FROM mytable WHERE TYPE = 'MSS' AND S = 0 AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE())
) as MSS_S UNION
(
SELECT COUNT(*) FROM mytable WHERE TYPE = 'MSS' AND S != 0 AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE())
) as MSS_F UNION
(
SELECT COUNT(*) as Nb ,OR as TOP FROM mytable WHERE TYPE = 'MSS'  AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) 
GROUP BY OR ORDER BY Nb DESC LIMIT 1
于 2013-05-14T07:35:16.183 回答
1

首先,您应该使用来连接结果,UNION其次是关键字,因此您需要对它们进行转义(假设您的表有一个名为 OR 的列):ORTOP

SELECT 
  COUNT(*) as Nb ,
  `OR` as `TOP` 
FROM 
  mytable 
WHERE 
  TYPE = 'MSS'  
  AND YEAR(date) = YEAR(CURDATE()) 
  AND MONTH(date) = MONTH(CURDATE()) 
GROUP BY 
  `OR` 
ORDER BY 
   Nb DESC 
LIMIT 1

此外,UNION 中的所有查询都必须具有相同数量的字段,并且前 1 个字段和后 2 个字段具有相同数量的字段

SELECT
  SUM(IF(`TOP` = 'one',`Nb`,0)) as first_one,
  SUM(IF(`TOP` = 'two',`Nb`,0)) as second_one,
  SUM(IF(`TOP` = 'three',`Nb`,0)) as thrid_one,
  SUM(IF(`TOP` NOT IN ('three','two','one'),`Nb`,0)) as forth_one,
  GROUP_CONCAT(IF(`TOP` NOT IN ('three','two','one'),`TOP`,'') SEPARATOR '') as `OR`
FROM (
    SELECT 
        COUNT(*) as Nb,
        'one' as `TOP` 
    FROM 
        mytable 
    WHERE 
        TYPE = 'MSS' 
        AND YEAR(date) = YEAR(CURDATE()) 
        AND MONTH(date) = MONTH(CURDATE()) 
    UNION ALL
    SELECT 
        COUNT(*) as Nb,
        'two' as `TOP` 
    FROM 
        mytable 
    WHERE 
        TYPE = 'MSS' 
        AND S=0
        AND YEAR(date) = YEAR(CURDATE()) 
        AND MONTH(date) = MONTH(CURDATE()) 
    UNION ALL
    SELECT 
        COUNT(*) as Nb,
        'three' as `TOP` 
    FROM 
        mytable 
    WHERE 
        TYPE = 'MSS' 
        AND S<>0
        AND YEAR(date) = YEAR(CURDATE()) 
        AND MONTH(date) = MONTH(CURDATE()) 
    UNION ALL
    SELECT
      `Nb`,
      `TOP`
    FROM(
      SELECT 
        COUNT(*) as Nb ,
        `OR` as `TOP` 
      FROM 
        mytable 
      WHERE 
        TYPE = 'MSS'  
        AND YEAR(date) = YEAR(CURDATE()) 
        AND MONTH(date) = MONTH(CURDATE()) 
      GROUP BY 
        `OR` 
      ORDER BY 
         Nb DESC 
      LIMIT 1
    ) as tmp
)as tmp1

UPDATE前 3 个查询可以恢复为:

SELECT 
    SUM(IF(S=0,1,0)) as MSS_S,
    SUM(IF(S<>0,1,0)) as MSS_F,
    COUNT(*) as TOTAL_MSS
FROM 
    mytable 
WHERE 
    TYPE = 'MSS' 
    AND YEAR(date) = YEAR(CURDATE()) 
    AND MONTH(date) = MONTH(CURDATE()) 
于 2013-05-14T07:37:26.893 回答