0

我想创建一个新闻更新系统,使用 MySQL 和 PHP。但我无法让它工作,加入语句和 PHP 有问题,所以我可以显示新闻更新和所有附加的评论(如果有效用户登录,该人可以单独删除每条评论)。但这只是我遇到问题的 JOIN 语句。

我有这些表和数据库架构

  1. news_tbl(news_id、日期、用户(fk to users_tbl.username)、标题、正文和图片)
  2. users_tbl(用户名、电子邮件、密码、用户类型)
  3. comments_tbl (comments_id, name, comment, news_id(fk to news_id))

我试过这个:

$sqlquery ="SELECT news_tbl.*, users_tbl.*, comments_tbl.*,
COUNT(comments_tbl.comments_id) AS comments_count
FROM news_tbl
LEFT JOIN users_tbl
ON news_tbl.user = users_tbl.username
LEFT JOIN comments_tbl
ON comments_tbl.news_id = news_tbl.news_id
GROUP BY news_tbl.news_id ";

但后来我只能显示一条评论,我想要所有评论,我想知道每条评论的 ID,所以用户可以单独删除每条评论。如果没有写评论,我也无法获得新闻ID?

4

1 回答 1

0

这应该大致得到您想要的,尽管您应该始终共享您的查询并准确指定您假装要实现的目标。

Select ut.user_id,nt.news_id,nt.headline, nt.body,ct.comments_id,
ct.comment,count(ct.comments_id) as total
from news_tbl nt
inner join users_tbl ut on ut.user_id=nt.user_id
left join comments_tbl ct on ct.news_id=nt.news_id
group by ut.user_id,nt.news_id

祝你好运。

于 2012-12-15T17:47:09.590 回答