gpt4 book ai didi

mysql - 在mysql查询中使用两个内连接

转载 作者:行者123 更新时间:2023-11-29 23:45:47 25 4
gpt4 key购买 nike

我有两个表格,如下图所示:

用户表:

enter image description here

offer_comments

enter image description here

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_comments,我该怎么办?我想使用下面的 sql 而不是 offer_comments :

select offer.*,u.id,u.name,u.family from offer_comments offer
inner join users u on offer.id=u.id

喜欢:

    $result_comments = mysql_query("select e.comment as comment,m.comment as answer_to_comment 
from (select offer.*,u.name,u.family from offer_comments offer
inner join users u on offer.id=u.id) e
inner join offer_comments m on e.id = m.quet
where e.offer_id=$id and e.confirm=1");

但它返回[]!!

最佳答案

试试这个:

   $result_comments = mysql_query("select e.comment as comment,m.comment as answer_to_comment 
from (select offer.*,u.name,u.family from offer_comments offer
inner join users u on offer.user_id=u.id) e
inner join offer_comments m on e.id = m.quet
where e.offer_id=$id and e.confirm=1");

此处的更改是在 e 派生表中,usersoffer_comments 之间的关系应为 users.id = Offer_comments。 user_id 并且您已完成users.id = Offer_comments.id

关于mysql - 在mysql查询中使用两个内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25959590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com