JOIN SQL查询结果为一

时间:2013-05-14 07:33:31

标签: mysql join union

我想查询几个select in one,我的查询在没有最后一个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

3 个答案:

答案 0 :(得分: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;

希望这会有所帮助......

答案 1 :(得分:1)

使用 UNION 喜欢

SELECT_STMT1 UNION SELECT_STMT2 UNION SELECT_STMT3 UNION SELECT_STMT4

AS

 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

答案 2 :(得分:1)

首先,您应该使用UNION加入结果,第二个ORTOP是关键字,因此您需要转义它们(假设您的表有一个名为OR的列) :

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

更新前三个查询可以恢复为:

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())