If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Selecting threads with the most-recent posts

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-08, 08:59
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
Selecting threads with the most-recent posts

Hi,

I need to show threads which are either new or which have new posts.

Code:
CREATE TABLE `coaching_threads` (
  `id` smallint(5) unsigned NOT NULL auto_increment,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `title` varchar(45) NOT NULL,
  `featured` bit(1) NOT NULL default '\0',
  `coaching_post_count` mediumint(8) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=46 DEFAULT CHARSET=latin1;

CREATE TABLE `coaching_posts` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `coaching_thread_id` smallint(5) unsigned NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `user_id` mediumint(8) unsigned NOT NULL,
  `body` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=latin1;
Any ideas?

I thought of trying to get the most recent posts (by created date) with a distinct coaching_thread_id, but I'm not sure how to write that exactly.

_da.
Reply With Quote
  #2 (permalink)  
Old 08-12-08, 09:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
please indicate exactly which columns you need from each table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-12-08, 09:43
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
Hi Rudy,

I need thread id, title, featured, coaching_post_count. I also need the user_id and body from the first post in the thread, but that can be done separately if necessary.

Regards,

_da.
Reply With Quote
  #4 (permalink)  
Old 08-12-08, 09:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT t.id
     , t.title
     , t.featured
     , t.coaching_post_count
     , p.user_id 
     , p.body 
  FROM coaching_threads AS p
LEFT OUTER
  JOIN ( SELECT coaching_thread_id
              , MAX(created) AS max_created
           FROM coaching_posts
         GROUP
             BY coaching_thread_id ) AS m
    ON m.coaching_thread_id = t.id
LEFT OUTER
  JOIN coaching_posts AS p
    ON p.coaching_thread_id = t.id
   AND p.created = m.max_created
by the way, why are created and modified both NOT NULL?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-12-08, 10:02
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
Didn't work unfortunately.

I'm using a PHP framework (CakePHP), which doesn't support OUTER JOINS, SUB SELECTS, etc., so I'd have to use a custom query, which would make paging etc. more difficult, but that's not the end of the world.

created and modified are NOT NULL because as soon as the record is created, it should have non-null dates in those two fields. A NULL value would have no meaning.

Thanks,

_da.
Reply With Quote
  #6 (permalink)  
Old 08-12-08, 10:06
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
If you do something like:

Code:
SELECT * from coaching_threads
JOIN coaching_posts
ON coaching_threads.id = coaching_posts.coaching_thread_id
ORDER BY coaching_posts.created desc
LIMIT 5
Is it possible to prevent duplicate thread IDs from being returned?

_da.
Reply With Quote
  #7 (permalink)  
Old 08-12-08, 10:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by darkangel
I'm using a PHP framework (CakePHP), which doesn't support OUTER JOINS, SUB SELECTS, etc.,
self := foot.shoot()

i'm not going to be able to help you

regarding the dates, well, i suppose you could consider that a post has been immediately modified the second it is created

however, i must disgree with you, a NULL in modified would have meaning -- it would mean the post has not been modifed
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 08-12-08, 10:47
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
Yes, I agree with regard to the modified date, but it's a framework thing again, the modified date is automatically set at creation time (which is pretty stupid actually).

Not the best framework, or even a programming language I'm that fond of, but that's what I have to use right now unfortunately.
Reply With Quote
  #9 (permalink)  
Old 08-12-08, 10:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
first of all, i know sql but not php (heck, i can barely spell php)

so i asked on another (more active) forum, and was given this link --

http://www.codeclippers.com/clipping...e:_FinderQuery

i must say that this whole "framework" idea smells awfully fishy if you can't write meaningful sql statements
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 08-12-08, 13:19
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
Thanks Rudy. Yes, you can write finder queries and custom queries, but I'm not sure how that will affect record paging and other framework functionality.

The query you wrote though didn't return the expected results.

Another option is to maintain an extra field in the thread table with the date of the last post ... I started with this but maintaining it was confusing when records were deleted, etc. ... I may have to return to this.

Do you know the answer to my question in post #6 above?

Appreciate your help.

_da.
Reply With Quote
  #11 (permalink)  
Old 08-19-08, 10:58
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
Rudy,

Do you think I should store a first_post_id and last_post_id in the thread table? Or any other data that is able to be selected from the existing data?

_da.

Last edited by darkangel; 08-19-08 at 13:49.
Reply With Quote
  #12 (permalink)  
Old 08-19-08, 13:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
should you? i don't know

adding redundant columns that have to be updated every time somebody posts sounds a lot like:
Code:
if self===foot.shoot() { self = wrists.slice() }
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 08-19-08, 13:50
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
I'm thinking about performance as well ... having to do a:

Code:
SELECT *
FROM coaching_posts AS p
WHERE p.coaching_thread_id = 123
ORDER BY p.created ASC/DESC
LIMIT 1
Just to retrieve the first/last post in a thread might not be the best idea.

_da.
Reply With Quote
  #14 (permalink)  
Old 08-19-08, 14:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
yeah, sorting the entire table just so that you can use LIMIT 1 on it, that seems excessive

what about a subquery?

WHERE created = ( SELECT MAX(created) FROM ... )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 08-19-08, 14:57
darkangel darkangel is offline
Registered User
 
Join Date: Feb 2005
Location: Cape Town, South Africa
Posts: 57
Good idea, I can use that in other areas. However, it doesn't really solve the problem I have regarding selecting the threads with the most-recent posts.

_da.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On