Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用“将一个表连接到自身并连接另一个表”_Mysql_Sql - Fatal编程技术网

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最好提供您的表结构和一些示例数据集@查看如何放置您的结构和数据