mysql - 如何从多个表中求和多个计数

标签 mysql sql count sum

希望有人能帮助我..

对于我的忠诚度计划,我会计算添加的歌曲数量、添加的类(class)数量、类(class)和歌曲的评论数量等等。

对于名人堂,我想对最高声誉的成员进行查看。并在头像下方添加声誉。

因此我想总结一下: TOTAL = 总歌曲数 + 总类(class)数 + 总歌曲评论数 + 总类(class)评论数

我的表格看起来像:

海报

poster_id| username | 
---------|----------|
1        | lisa
2        | john
3        | ben

类(class)

lesson_id| title    | poster_id
---------|----------|----------
1        | lesson1  | 1
2        | lesson2  | 1
3        | lesson3  | 2
4        | lesson4  | 3
5        | lesson5  | 1
6        | lesson7  | 2

歌曲

song_id  | title    | poster_id
---------|----------|----------
1        | song 1   | 1
2        | song 2   | 1
3        | song 3   | 2

歌曲评论

com_id   | song_id  | poster_id | comment
---------|----------|-----------|--------
1        | 1        | 1         | This comment1
2        | 2        | 1         | This comment2
3        | 3        | 2         | This comment3

类(class)评论

com_id   | lesson_id| poster_id | comment
---------|----------|-----------|--------
1        | 1        | 1         | This comment1
2        | 2        | 1         | This comment2
3        | 3        | 2         | This comment3

请帮助设置mysql查询

    SELECT poster.gebruikersnaam 
    SUM(
        (SELECT COUNT(*) FROM song) AS totalsongs +
        (SELECT COUNT(*) FROM lesson AS totallesson + 
        (SELECT COUNT(*) FROM songcomment AS totalsongcomments +
        (SELECT COUNT(*) FROM lessoncomment AS totallessoncomments +
        )
    FROM song
    INNER JOIN poster ON poster.poster_id = song.song_poster_id
    WHERE song.song_poster_id !=  '0'
    GROUP BY poster.poster_id
    ORDER BY TOTAL
    LIMIT 0 , 250

这没问题,但是.. 仍然必须按总和订购 :)

SELECT P.poster_id,
  (SELECT COUNT(*) FROM song WHERE P.poster_id = song.song_poster_id) AS SongCount,
  (SELECT COUNT(*) FROM lesson WHERE P.poster_id = lesson.lesson_poster_id) AS LessonCount,
  (SELECT COUNT(*) FROM commentaar WHERE P.poster_id = commentaar.poster_id) AS SongCommCount,
  (SELECT COUNT(*) FROM lesson_comment WHERE P.poster_id = lesson_comment.lesson_comment_poster_id) AS LessonCommCount
FROM poster AS P
LIMIT 0, 50

编辑 2

    SELECT PM.poster_id , PM.SongCount , PM.LessonCount, PM.SongCommCount, PM.LessonCommCount, (PM.SongCount + PM.LessonCount + PM.SongCommCount + PM.LessonCommCount) AS TotalCount 
FROM (
  SELECT P.poster_id, 
    (SELECT COUNT(*) FROM song WHERE P.poster_id = song.song_poster_id) AS SongCount, 
    (SELECT COUNT(*) FROM lesson WHERE P.poster_id = lesson.lesson_poster_id) AS LessonCount, 
    (SELECT COUNT(*) FROM commentaar WHERE P.poster_id = commentaar.poster_id) AS SongCommCount, 
    (SELECT COUNT(*) FROM lesson_comment WHERE P.poster_id = lesson_comment.lesson_comment_poster_id) AS LessonCommCount 
  FROM poster AS P 
  LIMIT 0, 50
) AS PM
ORDER BY (PM.SongCount + PM.LessonCount + PM.SongCommCount + PM.LessonCommCount) DESC

编辑 3

    SELECT poster_id, 
       songCount, lessonCount, songCommentCount, lessonCommentCount,
       songCount + lessonCount + songCommentCount + lessonCommentCount as totalRank
FROM(SELECT poster.poster_id, 
            COALESCE(song.count, 0) as songCount,
            COALESCE(lesson.count, 0) as lessonCount,
            COALESCE(commentaar.count, 0) as songCommentCount,
            COALESCE(lesson_comment.count, 0) as lessonCommentCount
     FROM poster
     LEFT JOIN (SELECT song_poster_id, COUNT(*) as count
                FROM song
                GROUP BY song_poster_id) song
            ON song.song_poster_id = poster.poster_id  
     LEFT JOIN (SELECT lesson_poster_id, COUNT(*) as count
                FROM lesson
                GROUP BY lesson_poster_id) lesson
            ON lesson.lesson_poster_id = poster.poster_id
     LEFT JOIN (SELECT poster_id, COUNT(*) as count
                FROM commentaar
                GROUP BY poster_id) commentaar
            ON commentaar.poster_id = poster.poster_id
     LEFT JOIN (SELECT lesson_comment_poster_id, COUNT(*) as count
                FROM lesson_comment
                GROUP BY lesson_comment_poster_id) lesson_comment
            ON lesson_comment.lesson_comment_poster_id = poster.poster_id) Total
ORDER BY totalRank DESC
LIMIT 0, 50

最佳答案

这是 SQL Fiddle显示以下查询确实有效。如您所见,我正在创建表格并用您在问题中拥有的数据填充它们。然后我执行下面的查询来收集你需要的数据。我确实验证了计数,它们计算正确。

SELECT PM.*, 
  (
    PM.SongCount + PM.LessonCount + 
    PM.SongCommCount + PM.LessonCommCount
  ) AS TotalCount 
FROM (
  SELECT P.poster_id, 
    (
      SELECT COUNT(poster_id) 
      FROM song S 
      WHERE P.poster_id = S.poster_id
    ) AS SongCount, 
    (
      SELECT COUNT(poster_id) 
      FROM lesson L 
      WHERE P.poster_id = L.poster_id
    ) AS LessonCount, 
    (
      SELECT COUNT(poster_id) 
      FROM SongComment SC 
      WHERE P.poster_id = SC.poster_id
    ) AS SongCommCount, 
    (
      SELECT COUNT(poster_id) 
      FROM LessonComment LC 
      WHERE P.poster_id = LC.poster_id
    ) AS LessonCommCount 
  FROM poster AS P 
  LIMIT 0, 50
) AS PM
ORDER BY 
  (
    PM.SongCount + PM.LessonCount + 
    PM.SongCommCount + PM.LessonCommCount
  ) DESC

关于mysql - 如何从多个表中求和多个计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184444/

相关文章:

MySQL返回一个Group By Functions表?

sql - 如何创建虚拟列并在连接后填充数组?

perl - 在 Perl 中,如何按值的频率排序?

mysql - 在 MYSQL Where 子句中使用 COUNT(*) AS `COUNT`

php - 有列 "num_posts"或查询数据库

php - 使用一个 SQL 请求更新多行

mysql - 如何使多个 If 语句起作用?

mysql - 日期转换为给定格式MYSQL

mysql - SQL - 为每个用户获取最高值(DISTINCT?)

mysql - 连接查询,具有基于列的分组依据和子计数