- Apr 19, 2001
- 25,284
- 1,997
- 126
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?
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?