将两个SQL查询合并为1

时间:2012-11-30 01:11:01

标签: mysql

我有一个简单的问题......

我想将两个sql查询合并为一个,但问题是这两个查询都包含GROUP BY。

查询1:

SELECT   COUNT(users_survey_answers.ultimate_survey_code)
FROM     company,
         users_codes,
         users_survey_answers
WHERE    company.company_id = users_codes.company_id
  AND    users_codes.email = "test@gmail.com"
  AND    users_codes.company_id = users_survey_answers.company_id
  AND    users_survey_answers.completed = 0
GROUP BY users_codes.company_id

查询2:

SELECT   COUNT(users_rewards.reward_id)
FROM     company, 
         users_codes, 
         users_rewards
WHERE    company.company_id = users_codes.company_id
  AND    users_codes.email = "test@gmail.com"
  AND    users_rewards.company_id = users_codes.company_id
  AND    redeemed = 0
GROUP BY users_codes.company_id

我很感激帮助。我尝试了很多解决方案,仍然没有帮助。

感谢。

2 个答案:

答案 0 :(得分:1)

我采用了将company_id添加到选择中的库。如果你不知道哪个结果属于哪个公司,那么按company_id分组有什么好处。 :)

<击>
select
  company_id,
  sum(code_count) as code_count,
  sum(reward_count) as reward_count
FROM
    (SELECT   
       users_codes.company_id,
       COUNT(users_survey_answers.ultimate_survey_code) as code_count,
       null as reward_count
    FROM     company,
             users_codes,
             users_survey_answers
    WHERE    company.company_id = users_codes.company_id
      AND    users_codes.email = "test@gmail.com"
      AND    users_codes.company_id = users_survey_answers.company_id
      AND    users_survey_answers.completed = 0
    GROUP BY users_codes.company_id
    UNION ALL
    SELECT   
      users_codes.company_id,
      COUNT(users_rewards.reward_id) as reward_count,
      null
    FROM     company, 
             users_codes, 
             users_rewards
    WHERE    company.company_id = users_codes.company_id
      AND    users_codes.email = "test@gmail.com"
      AND    users_rewards.company_id = users_codes.company_id
      AND    redeemed = 0
    GROUP BY users_codes.company_id) x
GROUP BY 
  company_id

<击>

太麻烦了。你可以像这样写。 我也冒昧地引入了更现代的INNER JOIN以及别名,但可以随意回复它。

SELECT
  c.company_id,
  (SELECT COUNT(ultimate_survey_code)
  FROM
    users_survey_answers a
  WHERE
    a.completed = 0 AND
    a.company_id = c.company_id) as answer_count,
  (SELECT COUNT(ultimate_survey_code)
  FROM
    users_rewards r
  WHERE
    r.redeemed = 0 AND
    r.company_id = c.company_id) as reward_count
FROM
  company c
  INNER JOIN users_code uc ON uc.company_id = c.company_id
WHERE
  uc.email = "test@gmail.com"
GROUP BY
  c.company_id

答案 1 :(得分:1)

试试这个:

SELECT    company.company_id,
          users_pts.pts as CompanyPts,
          t1.ultimate_survey_code_count,
          t2.reward_id_count
FROM      company,
          users_codes
INNER JOIN (
          SELECT 
              company_id, 
              COUNT(ultimate_survey_code) AS ultimate_survey_code_count
          FROM users_survey_answers
          WHERE completed = 0
          GROUP BY company_id
          ) AS t1
       ON users_codes.company_id = t1.company_id
INNER JOIN (
          SELECT 
              company_id, 
              SUM(IF(redeemed = 0, 1, 0)) AS reward_redeem_zero_count,
              SUM(IF(redeemed = 0, 0, 1)) AS reward_redeem_not_zero_count
          FROM users_rewards
          GROUP BY company_id
          ) AS t2
       ON users_codes.company_id = t2.company_id
INNER JOIN users_pts
       ON users_codes.company_id = users_pts.company_id
WHERE     company.company_id = users_codes.company_id
  AND     users_codes.email = "test@gmail.com"

这是未经测试的,但希望符合您的需求。