-2

簡単な質問があります...

2つのSQLクエリを1つに結合したいのですが、問題は、これらのクエリの両方にGROUPBYが含まれていることです。

クエリ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

私は助けに感謝します。私は非常に多くの解決策を試しましたが、それでも助けにはなりません。

ありがとう。

4

2 に答える 2

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
于 2012-11-30T01:19:45.170 に答える
1

Try this:

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"

This is untested, but hope meets your need.

于 2012-11-30T01:31:48.227 に答える