Is there some great MySQL function of flash of insight someone could recommend for the following scenario?
I have a table where I need to select and order rows by timestamp field. In this talbe I have a col called 'start': it can be 1 or 0.
I'd like all the rows ordered by date BUT only if they have a start of 1
id ¦ title ¦ desc ¦ date ¦ start
(or somthing similar)...
It's easy to get a list of 'topics' (it's for forum software) as start=1 means theat it's the first post, but I'm trying to get it ordered by last reply which means currnetly I get dupe values as I cannot restrain it by 'start = 1' .
Anyone following that? ;-)
Nick
Sorry, let me clarify: The records that have start=o (replies to topics) are what I want to order by (the most recent posts)...
>DISTINCT
I'll go have a look ;-)
<added>for anyone thinking of re-doing the table, it's just an example ;) the actual setup is more complex but this illustrates the point reasonably...</added>
Nick
I think you need to have two tables.
Say Threads and Messages... where threads is the master table and messages is the detail table.
Threads
thread_id¦subject¦description
Messages
message_id¦thread_id¦message¦start¦timestamp
Now if this schema looks OK for what you are writing...do you need to select threads with latest posts... or is it some thing else..
CREATE TABLE postcontrols (
postid INT UNSIGNED NOT NULL,
start TINYINT(1) UNSIGNED NOT NULL,
date TIMESTAMP NOT NULL,
parent INT UNSIGNED NOT NULL,
author INT UNSIGNED NOT NULL,
INDEX(start),
INDEX(date),
INDEX(parent),
PRIMARY KEY(postid)
);
The postid connects to a table containing the text of a post, the parent refers to the thread that the post belongs to.
If start is '1' then it is the first post of a thread, ohterwise it is a reply.
I can get an index of threads ordered by the date the thread was started, but when I try to get the 'active list' effect, I get many dupe values....
Does that make it clearer? ;-)
Nick
So if I have
GROUP BY parent
ORDER BY date DESC LIMIT 3.30
I get no dupes but no 'ative list' order either...
and
ORDER BY date DESC
GROUP BY parent LIMIT 3.30
does not work....
Am I doing somthing wrong here?
Nick
WHERE threadcontrols.forum = '1' AND threadcontrols.threadid = threads.id AND postcontrols.parent = threads.id AND threadcontrols.status > 2 AND postcontrols.author = members.id GROUP BY postcontrols.parent ORDER BY postcontrols.date DESC LIMIT 0,20
Nick