1

誰かが私を助けてくれることを願っています..

私のロイヤルティ プログラムでは、追加される曲の量、追加されるレッスンの量、レッスンや曲に関するコメントの量などを数えます。

殿堂入りについては、最も評判の良いメンバーを紹介したいと思います。また、アバターの下に評判を追加します。

したがって、合計したいと思います: TOTAL = totalsongs + totallesson + totalsongcomments + totallessoncomments

私のテーブルは次のようになります。

ポスター

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

レッスン

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

レッスンコメント

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

これはOKEですが..まだ合計で注文する必要があります:)

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
4

2 に答える 2

0

これは、以下のクエリが実際に機能することを示す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
于 2013-10-04T15:06:26.123 に答える