• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help with a MySQL query please

Trying to jury-rig a forum with a built-in shoutbox to get the shouts to display on the front page of the site rather than on the forum itself.

The shouts are stored in a table called 'shouts' with the following data

shid - the unique id of the shout itself, also the PRIMARY KEY of the table
shtime - the timestamp
shuid - the id # of the user that delivered the shout
shtext - the shouted message

I can easily retrieve that data and display the 5 or 10 most recent shouts on the front page of the site. That, alas, is the extent of my SQL query-ing skills. The users name isn't stored in that table. It's in a table named 'users' with dozens of fields, only 2 of which matter


id - the id of the user, it equates to the shuid field in the 'shouts' table
username - the screen name of the user


I know I can kludge it and loop through the shuid to compare it to the 'id' field in the other table to get the username that matches up to it. But I also know that's an inefficient way to do it and would hit the database server harder than it needs to be hit. What's the actual programmers way to write that query and display the results like

username - shtime - shtext

for the 5 most recent shouts?
 
You need to do an inner join.

Code:
SELECT username, shtime, shtext FROM shouts INNER JOIN users ON shouts.shuid = users.id ORDER BY shtime DESC LIMIT 5;
 
Back
Top