Php 在MYSQL中从表中选择行两次

Php 在MYSQL中从表中选择行两次,php,mysql,join,Php,Mysql,Join,没有解决我问题的相关问题: 我目前正在尝试为我的网站创建一个评论区,用户可以在这里添加评论并回复其他用户的评论 因此,每当我将注释保存到数据库时,我都会添加以下数据: 文本:用户输入的文本 问题ID:指我网站上的子网站的ID 用户ID:发送评论的用户的用户ID 回复ID:此评论回复的评论ID 表格和结果应该是这样的 现在,当我试图获取某个特定站点的所有评论时,包括撰写回复栏中提到的评论的用户的姓名,我碰到了一堵墙 到目前为止我所做的: SELECT comment.id,

没有解决我问题的相关问题:

我目前正在尝试为我的网站创建一个评论区,用户可以在这里添加评论并回复其他用户的评论

因此,每当我将注释保存到数据库时,我都会添加以下数据:

  • 文本:用户输入的文本
  • 问题ID:指我网站上的子网站的ID
  • 用户ID:发送评论的用户的用户ID
  • 回复ID:此评论回复的评论ID
表格和结果应该是这样的

现在,当我试图获取某个特定站点的所有评论时,包括撰写回复栏中提到的评论的用户的姓名,我碰到了一堵墙

到目前为止我所做的:

SELECT comment.id,
       comment.reply_to,
       comment.text,
       comment.date,
       comment.user_id,
       users.name,
       users.user_group,
       user_group.name AS group_name,
       users_to.name AS reply_to_username,
FROM COMMENT,
     users,
     user_group
JOIN COMMENT AS comment_to ON COMMENT.reply_to = comment_to.id
JOIN users AS users_to ON comment_to.user_id = users.id
WHERE COMMENT.question_id = 1
  AND COMMENT.user_id = users.id
  AND users.user_group = user_group.id
  AND COMMENT.id <> 0
ORDER BY COMMENT.date DESC
选择comment.id,
回复:,
comment.text,
评论日期:,
comment.user\u id,
users.name,
users.user_组,
user_group.name作为组名称,
用户\u to.name作为回复\u to\u用户名,
从评论来看,
用户,
用户组
在COMMENT.reply\u to=COMMENT\u to.id中将COMMENT作为COMMENT\u加入COMMENT
将用户作为用户加入到注释中。用户\u id=users.id
其中COMMENT.question_id=1
和COMMENT.user_id=users.id
和users.user\u group=user\u group.id
和COMMENT.id 0
按注释排序。日期描述
我遇到的问题是,它总是告诉我comment.reply\u to是一个未知列

我甚至可以用SELECT语句连接当前选择的同一个表。
我知道我可以用两个单独的语句来实现这一点,但出于性能原因,我宁愿这样做

这对你有用吗?有额外的逗号和注释_肯定是未定义的

SELECT
a.id,
a.reply_to,
a.text,
a.date,
a.user_id,
b.name,      
b.user_group,
c.name as group_name,
users_to.name   as reply_to_username
FROM comment a
INNER JOIN users b ON a.user_id = b.id
INNER JOIN user_group c ON b.user_group = c.id
INNER JOIN comment as comment_to ON a.reply_to = comment_to.id
INNER JOIN users   as users_to ON comment_to.user_id = users.id
WHERE a.question_id = 1
AND a.id  <> 0
ORDER BY a.date DESC
选择
a、 身份证,
a、 答复:,
a、 文本,
a、 日期,
a、 用户id,
b、 名字,
b、 用户组,
c、 名称作为组名称,
用户\u to.name作为回复\u to\u用户名
来自评论a
a.user\u id=b.id上的内部联接用户b
b上的内部联接用户组c。用户组=c.id
在a.reply\u to=comment\u to.id上将内部连接注释作为注释\u to
内部将用户作为用户加入到注释中。用户\u id=users.id
其中a.question_id=1
a.id为0
按日期说明订购
选择
a、 身份证,
a、 答复:,
a、 文本,
a、 日期,
a、 用户id,
b、 名字,
b、 用户组,
c、 名称作为组名称,
用户\u to.name作为回复\u to\u用户名
来自评论a
a.user\u id=b.id上的内部联接用户b
b上的内部联接用户组c。用户组=c.id
在a.reply\u to=comment\u to.id上将内部连接注释作为注释\u to
内部将用户作为用户加入到注释中。用户\u id=用户\u到.id
其中a.question_id=1
a.id为0
按日期说明订购
这解决了我的问题。现在一切都按预期返回。
谢谢你的帮助。

不要混合加入风格<代码>从x,y,z连接是可怕的黑客行为。选择一种风格并坚持下去。这里没有php来支持这个问题,而且你有一个语法错误。检查错误会告诉你这一点。“comment_positive是未定义的”-嗯?必须将内部连接用户更改为ON comment_to.user_id=users.id到内部连接用户为users_to ON comment_to.user_id=users_to.id,但除此之外,它还能工作!谢谢,没想到会这么快得到答复。
SELECT
a.id,
a.reply_to,
a.text,
a.date,
a.user_id,
b.name,      
b.user_group,
c.name as group_name,
users_to.name   as reply_to_username
FROM comment a
INNER JOIN users b ON a.user_id = b.id
INNER JOIN user_group c ON b.user_group = c.id
INNER JOIN comment as comment_to ON a.reply_to = comment_to.id
INNER JOIN users   as users_to ON comment_to.user_id = users_to.id
WHERE a.question_id = 1
AND a.id  <> 0
ORDER BY a.date DESC