0

これらの2つのクエリを1つのクエリに結合して、結果が次のようになるようにする必要がありましたか。

ChannelId   | ContentType | ContentTypeCount | SumOfAllContentTypes
  100       | link        | 59               | 179
  100       | photo       | 49               | 179
  100       | status      | 2                | 179
  100       | video       | 4                | 179
  101       | link        | 15               | 179
  101       | status      | 50               | 179

現在使用しているクエリは次のとおりです。

SELECT
COUNT(posts.id)
FROM posts
INNER JOIN channels ON channels.id = posts.channel_id
WHERE channels.site_id = 1003
AND channels.channel_type_id = 1

結果=179

と..

SELECT
posts.channel_id,
posts.contenttype,
COUNT(posts.contenttype) as contenttypecount
FROM posts
INNER JOIN channels ON channels.id = posts.channel_id
WHERE channels.site_id = 1003
AND channels.channel_type_id = 1
GROUP BY posts.channel_id, posts.contenttype

結果=100| リンク| 59; 等..

よろしくお願いします。

4

1 に答える 1

2

これを試して:

select A.*, B.*
from (
  SELECT
  posts.channel_id,
  posts.contenttype,
  COUNT(posts.contenttype) as contenttypecount
  FROM posts
  INNER JOIN channels ON channels.id = posts.channel_id
  WHERE channels.site_id = 1003
  AND channels.channel_type_id = 1
  GROUP BY posts.channel_id, posts.contenttype
) A
join (
  SELECT
  COUNT(posts.id) as Total
  FROM posts
  INNER JOIN channels ON channels.id = posts.channel_id
  WHERE channels.site_id = 1003
  AND channels.channel_type_id = 1
) B on 1=1

特に効率的ではありませんが、簡単でシンプルです。

于 2013-03-13T18:48:08.353 に答える