2

PHPとMYSQLを使用して、コメント付きのTwitterタイプのフィードを実現しようとしています。

Tweet1
  - Comment1 about Tweet1
  - Comment2 about Tweet1
Tweet2
  - Comment1 about Tweet2
  - Comment2 about Tweet2
  - Comment3 about Tweet2
Tweet3
  - Comment1 about Tweet3

各ツイートの下に関連するコメントを含む「ツイート」をループアウトすることに成功しています。ただし、私の問題は、その特定のツイートに対するコメントの数ごとに「ツイート」をループアウトしていることです。

tweet 1
  -comment 1 about tweet1
tweet 1
  -comment 1 about tweet 1
  -comment 2 about tweet 1 (Tweet 1 has 2 comments so loops out tweet 1 two times)
tweet 2
  -comment 1 about tweet 2
tweet 2
  -comment 1 about tweet 2
  -comment 2 about tweet 2
tweet 2
  -comment 1 about tweet 2
  -comment 2 about tweet 2
  -comment 3 about tweet 2 (Tweet 2 has 3 comments so loops out tweet 2 three times)

結合を使用するのは初めてなので、結合mysqlクエリに問題があると思います

"SELECT tweets.accountno, tweets.id,tweets.tweet,tweets.createdat,tweets.userid,users.name,users.avatar,users.biog,comments.comment FROM tweets INNER JOIN users ON tweets.userid = users.id JOIN comments ON tweets.id = comments.tweetid WHERE tweets.accountno ='123456' ORDER by tweets.ID DESC LIMIT 20"

アイデアはありますか?とても有難い

4

1 に答える 1

0

a を追加するGROUP BY tweets.idと、問題ないはずです。

SQLステートメントの書き方は次のとおりです。簡単にするために、明示的なJOINステートメントを削除し、暗黙的なステートメントを使用しています

SELECT 
    tweets.accountno, 
    tweets.id,
    tweets.tweet,
    tweets.createdat,
    tweets.userid,
    users.name,
    users.avatar,
    users.biog,
    comments.comment 
FROM 
    tweets, users, comments
WHERE
    tweets.userid = users.id
AND
    tweets.id = comments.tweetid 
AND
    tweets.accountno ='123456' 
GROUP BY
    tweets.id
ORDER BY
    tweets.id
DESC 
LIMIT
    20
于 2012-04-25T19:04:31.690 に答える