Mysql 使用“将一个表连接到自身并连接另一个表”
我有两张桌子,如下图所示: 用户表: 发表意见Mysql 使用“将一个表连接到自身并连接另一个表”,mysql,sql,Mysql,Sql,我有两张桌子,如下图所示: 用户表: 发表意见 offer_comments表存储评论和对评论的回答。 通过使用下面的功能,我可以根据$id获取评论并回答评论 function getCommentsAnItem($id){ mysql_query("SET CHARACTER SET utf8"); $result_comments = mysql_query("select e.comment as comment,m.comment as answer_to_comm
offer_comments
表存储评论和对评论的回答。
通过使用下面的功能,我可以根据$id
获取评论并回答评论
function getCommentsAnItem($id){
mysql_query("SET CHARACTER SET utf8");
$result_comments = mysql_query("select e.comment as comment,m.comment as answer_to_comment
from offer_comments e
inner join offer_comments m on e.id = m.quet
where e.offer_id=$id and e.confirm=1");
$comments = array();
while($a_comment=mysql_fetch_object($result_comments)){
$comment = array(
'comment'=>$a_comment->comment,
'answer'=>$a_comment->answer_to_comment
);
array_push($comments,$comment);
}
return $comments ;
}
根据offer\u comments
表中的user\u id
,如何从users
表中获取name
和family
更新: admin是其中的user_id equal=1
user_id 14690
是一个用户,会给他留下评论和管理员回复
在用户
表中:
我想获得一系列评论,并根据$id回答评论:
"comment=>how a u ?","answer=> I am fine","comment.name=>name","comment.family=>family"
使用别名为
m
的offer\u comments
将您的用户表与当前查询连接起来,这样它将显示属于m.comment
或添加m.comment
的用户的姓名和家庭,如果您想向offer\u comments
的用户显示化名为e
的评论,请加入e.user\u id
SELECT
e.comment AS `comment`,
m.comment AS answer_to_comment,
u.name,
u.family
FROM
offer_comments e
INNER JOIN offer_comments m
ON e.id = m.quet
INNER JOIN users s
ON s.id = m.user_id
WHERE e.offer_id = $id
AND e.confirm = 1
@S.M_Emamian您对每条评论都有用户id吗?并将用户的内部联接表更改为左联接表“您是否有针对每个注释的用户id?”是的,请等待更新问题。@S.M_Emamian最好提供您的表结构和一些示例数据集@查看如何放置您的结构和数据