php - Mysql Query Join Table on the most recent row -
i have page of categories, worked fine until did join.
the categories display so:
category 1-----------------------0----------0
discussion 1 someonecategory 2-----------------------0----------0
discussion 2 someoneelse
now says discussion, need display last discussion posted category based on it's discussion_id
. have tried order ... desc
sorts category names not discussion names , posted by.
$sql = "select *, count(d.cat_id) count discussions d left join categories c on (c.cat_id = d.cat_id) right join soldiers s on (s.uid = d.discussion_poster) group d.cat_id"; $result = query($sql); while (($row = mysqli_fetch_assoc($result)) != false) { $cat_id = $row['cat_id']; $discussion_id = $row['discussion_id']; $cat_title = $row['cat_title']; $discussion_title = $row['discussion_title']; $discussion_time = $row['discussion_time']; $count = $row['count']; $discussion_poster_id = $row['discussion_poster']; $discussion_poster = $row['soldier']; }
$sql = " select * categories c inner join ( select max(discussion_id) discussion_id, count(discussion_id) count, cat_id discussions group cat_id ) d1 on (c.cat_id = d1.cat_id) inner join discussions d on (d1.discussion_id = d.discussion_id) inner join soldiers s on (s.uid = d.discussion_poster) group d.cat_id";
Comments
Post a Comment