I have 4 tables:
The table Forumberichten (messages) contains the id's of the Discussion (discussions) table as a reference to reactions on a discussion. If there are 5 reactions to a discussion, there are 5 id's in the table with the same value.
How do I display an echo of one discussion of every different discussion with the latest reaction on it?
I've tried ORDER BY, DISTICT, GROUP BY and other things but I can't get it to work. I keep getting more outputs of the same discussion or one discussion with the original input and not the latest reaction to it.
<?php
// DISCUSSIES FORUM SCHRIJVEN
$sql="SELECT forumberichten.discussieid,forumberichten.datum,discussies.discussieid,discussies.titel,discussies.forumid,discussies.highlight,forums.forumid,forums.forumnaam,users.userid,users.userimage,users.username FROM forumberichten,discussies,forums,users WHERE forumberichten.discussieid=discussies.discussieid AND discussies.forumid=forums.forumid AND forumberichten.userid=users.userid AND forums.forumid=1 ORDER BY forumberichten.bericht_id DESC LIMIT 3";
$result = $conn->query($sql) or die ("The query could not be completed. try again");
if ($result->num_rows > 0) {
echo "
<br>
<table id='forumschrijvenklein'>
<tbody>
<tr>
<td bgcolor='#1E1E1E'></td>
<td nowrap bgcolor='#1E1E1E'> </td>
<td nowrap bgcolor='#1E1E1E' class='sterrenkleur'>Discussie</td>
<td nowrap bgcolor='#1E1E1E' class='sterrenkleur'>Laatste reactie</td>
<td nowrap bgcolor='#1E1E1E' class='sterrenkleur'>Datum</td>
<td nowrap bgcolor='#1E1E1E' class='sterrenkleur'></td>
</tr>
";
// output data of each row
while ($row = $result->fetch_assoc()) {
echo"
<tr>";if ($row["highlight"] == '1') {
echo " <td bgcolor='#FC6'></td>";
} else {
echo "<td bgcolor='#1E1E1E'></td>";
}
echo"
<td nowrap bgcolor='#1E1E1E'>";if ($row["userimage"] == '') {
echo "
<a href='user.php? id=" . $row['userid'] . "'</a><img src='Images/users/nopicture.png' alt='nopicture' class='userimage-tooltip-small' title='".$row['username']."''></a>";
} else {
echo "<a href='user.php? id=" . $row['userid'] . "'</a><img src='Images/users/".$row['userimage']."' class='userimage-tooltip-small' title='".$row['username']."''></a>";
}
echo"</td>
<td bgcolor='#1E1E1E'><a href='discussie.php? id=" . $row['discussieid'] . "'>".$row["titel"]."</a></td>
<td bgcolor='#1E1E1E'><a href='user.php? id=" . $row['userid'] . "'</a>".$row["username"]."</a></td>
<td nowrap bgcolor='#1E1E1E'>"; echo date("d-m-y H:i",strtotime($row["datum"]));"
";
echo"
</td>
<td nowrap bgcolor='#1E1E1E'></td>
</tr>
";
}
}
echo "
<tr>
<td bgcolor='#1E1E1E'> </td>
<td bgcolor='#1E1E1E'> </td>
<td bgcolor='#1E1E1E'><img src='Images/lijntransparant.png' width='315' height='2'></td>
<td bgcolor='#1E1E1E'><img src='Images/lijntransparant.png' width='140' height='2'></td>
<td bgcolor='#1E1E1E'> </td>
<td bgcolor='#1E1E1E'> </td>
</tr>
</tbody>
</table>
";
}
?>
I tried the following, but it still doesn't work...:
$sql = "SELECT *
FROM (((forumberichten
JOIN discussies
ON discussies.discussieid = forumberichten.discussieid)
JOIN users
ON users.userid = forumberichten.userid)
JOIN forums
ON forums.forumid=forumberichten.forumid)
GROUP BY discussies.discussieid
HAVING forumberichten.forumid = 3 AND forumberichten.bericht_id = max(forumberichten.bericht_id)
ORDER BY forumberichten.bericht_id DESC";
We want the newest post on each unique thread, we can do that by...
Using GROUP BY
and HAVING
.
SELECT *
FROM Posts
JOIN Discussions
on Discussions.discussion_id = Posts.discussion_id
GROUP BY Discussions.discussion_id
HAVING Posts.post_time = max(Posts.post_time)
ORDER BY Posts.post_time DESC
This is an example ! Make sure you use it to modify your own, it's quite simple though. I don't have a database to test this, but I am tested with my own data and different columns and it worked.
Note: This assumes that each Id is identical in both tables. It also assumes that post_time is some time value that increases the more recent the post is (ie SQL Timestamp).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.