Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
MySQL forum; complicated query ordering.
Old 07-17-2007, 07:25 PM MySQL forum; complicated query ordering.
Skilled Talker

Posts: 62
Name: Daniel
Trades: 0
First off, the query that I'm using is this:
PHP Code:
$query "SELECT 
 threads.id, 
 threads.subject,
 threads.timestamp,
 categories.name, 
 started.id AS starterid,
 started.username AS starter, 
 lastreply.id AS lastreplyid,
 lastreply.username AS lastreply
 FROM `threads`
 LEFT JOIN `users` AS started ON started.id = threads.started 
 LEFT JOIN `users` AS lastreply ON lastreply.id = threads.last_reply 
 LEFT JOIN `categories` ON categories.id = threads.category
 LIMIT 0,30"

What that does is get all the needed information from 4 tables to show the threads on the index.php page.

Im assuming most/all of you know how a forum generally works; and what I'd like to do is order the threads by according to a linked value from another table.

Example:
Code:
+----+-------------------------+---------+----------+------------+
| id | subject                 | started | category | timestamp  |
+----+-------------------------+---------+----------+------------+
|  1 | First Thread in Forum.  |       1 |        1 | 1184270342 |
|  2 | Second Thread in Forum. |       2 |        2 | 1184337553 |
|  3 | Third Thread in Forum.  |       1 |        2 | 1184690571 |
+----+-------------------------+---------+----------+------------+
Thats the threads table. and the posts are linked to the threads via the id. I'd like to take that and make a sql query much like this.

SELECT * FROM threads SORT BY posts.timestamp;

I know you cant do that, but thats as close as i can think.
castis is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-20-2007, 11:47 PM Re: MySQL forum; complicated query ordering.
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
If you already have the timestamp in the result-set, why don't you add the sort clause to the end of the join clause?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 07-24-2007, 07:20 PM Re: MySQL forum; complicated query ordering.
Skilled Talker

Posts: 62
Name: Daniel
Trades: 0
In the posts table you have id, thread_id, and timestamp. I wanted to order the threads by their corresponding rows in the posts table with the highest timestamp.

So that they'd automatically be sorted when someone posts a new reply.
castis is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to MySQL forum; complicated query ordering.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41683 seconds with 12 queries