我正在尝试使用 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"
有人有什么想法吗?非常感谢
添加一个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