sql - using two inner join in mysql query -
i have 2 tables below pictures :
users table :
offer_comments
offer_comments
table stores comments , answer comments. using below function can comments , answer comments depending on $id
.
function getcommentsanitem($id){ mysql_query("set character set utf8"); $result_comments = mysql_query("select e.comment comment,m.comment answer_to_comment offer_comments e inner join offer_comments m on e.id = m.quet e.offer_id=$id , 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 ; }
now,i use inner join instead of offer_comments
, how can ? use below sql instead of offer_comments
:
select offer.*,u.id,u.name,u.family offer_comments offer inner join users u on offer.id=u.id
like :
$result_comments = mysql_query("select e.comment comment,m.comment answer_to_comment (select offer.*,u.name,u.family offer_comments offer inner join users u on offer.id=u.id) e inner join offer_comments m on e.id = m.quet e.offer_id=$id , e.confirm=1");
but returns []
!!
try this:
$result_comments = mysql_query("select e.comment comment,m.comment answer_to_comment (select offer.*,u.name,u.family offer_comments offer inner join users u on offer.user_id=u.id) e inner join offer_comments m on e.id = m.quet e.offer_id=$id , e.confirm=1");
the change here in e
derived table relation between users
, offer_comments
should users.id = offer_comments.user_id
, you've done users.id = offer_comments.id
Comments
Post a Comment