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 > Help with JOIN

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-16-04, 01:19
ewarning ewarning is offline
Registered User
 
Join Date: May 2004
Posts: 1
Help with JOIN

Hi.

I'm writing a forum-like site, which has only one table for both posts and replies.

id INT(5), title, parent INT(5), for example.

If the user post a new topic, the id is auto-incremented and parent is 0.
If the user replies to an existent topic, id is 0 and parent is the id of the main topic.

now the question:

I'm trying to find a query that returns ONLY the main posts (id=0), without the replies. BUT it must be in order of updated items. If I post a new topic, it must be returned first in the query. If I post a reply to an old topic, this topic must comes first.

the code that I'm trying is:

SELECT DISTINCT parent.* FROM table AS parent
LEFT JOIN table AS son ON son.sub=parent.id
WHERE ((son.sub=parent.id OR parent.sub=0) AND parent.tipo=$p_id)
ORDER by son.data DESC, parent.data DESC

it doesn't work very well. there are such cases in that the last item inserted doesn't come first. i didn't find the error nor the logic of the values returned.

can someone help me on this?
thanks

ps: i've found that DISTINCT clause removes the duplicates, but doesn't keep the right order.
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