Jump to content

[SOLVED] Ordering a joined table


Anzeo

Recommended Posts

Hey,

 

 

 

I'm currently working on my own forum and so far everything's working. I have made four tables, CATEGORY, FORUM, TOPIC, POST.

I've completed the category part of my forum and I am almost done with the forum coding, but there's one question which I can't answer.

 

I want to order my posts/topic on date,time (in that order), but I have no clue whatsoever on how to do this. I mean, when a new topic is made it's added to the TOPIC table (so no data added in the POST table) and so the topic's date is stored in the TOPIC table, but a reply is added to the POST table and it's date and time are stored in the POST table.

 

How do I have to make my query in order for it to sort my posts and topics on date? (like on this forum for example).

 

Any help greatly appreciated.

 

 

 

Regards,

Anzeo

Link to comment
Share on other sites

Hey,

 

 

Thanks for the reply, but I tried it out and it's giving me this error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 WHERE TOPIC.ID = 1 ORDER BY POST.Datum DESC, POST.Tijd DESC' at line 1

 

and this is the query I wrote

 

$tpcqry = "SELECT * FROM TOPIC INNER JOIN POST USING $FID WHERE TOPIC.ID = 1 ORDER BY POST.Datum DESC, POST.Tijd DESC";

 

any idea what I'm doing wrong here?

Link to comment
Share on other sites

<?php
$tpcqry = "SELECT * FROM TOPIC INNER JOIN POST ON (TOPIC.ID = POST.TOPICID) WHERE TOPIC.ID = " . mysql_escape_string($id). " ORDER BY POST.Datum DESC, POST.Tijd DESC";
?>

 

POST.TOPICID should be whatever you use in the POST table to realate the post to a topic.

 

Best,

 

Patrick

Link to comment
Share on other sites

I misunderstood your problem.  You want something like this:

 


SELECT * FROM
(
   SELECT * FROM TOPIC 
   INNER JOIN POST ON (TOPIC.ID = POST.TOPICID) 
   GROUP BY TOPIC.ID HAVING (MAX(POST.POST.Datum))
) AS topics
ORDER BY topics.Dautm DESC, topics.Tijd DESC

 

Patrick

Link to comment
Share on other sites

Hey Patrick,

 

thanks for your help and time so far.

 

I've now wrote my query as:

$tpcqry = "SELECT * FROM ( SELECT * FROM TOPIC INNER JOIN POST ON TOPIC.ID = POST.Topic GROUP BY TOPIC.ID HAVING (MAX(POST.Datum))) AS TOPICS ORDER BY TOPICS.Datum DESC, TOPICS.Tijd DESC";

 

And this is the output  I get:

Column 'TOPICS.Datum' in order clause is ambiguous

 

Could this problem be caused because of the fact that I have a Datum column in both my TOPIC as my POST table?

 

 

 

Regards,

Anzeo

 

 

Link to comment
Share on other sites

Yes, you'll need to explicitly alias the columns you want in the inner select:

 

$tpcqry = "SELECT * FROM ( SELECT TOPIC.Datum as td, POST.Datum as pd, etc..etc.. FROM TOPIC INNER JOIN POST ON TOPIC.ID = POST.Topic GROUP BY TOPIC.ID HAVING (MAX(POST.Datum))) AS TOPICS ORDER BY TOPICS.pd DESC, TOPICS.Tijd DESC";

Link to comment
Share on other sites

I think it's working :D I need to alias all my colums though, but I think it's ok.

 

Thank you very much for your time and great help! I'll reply when I checked everything (I unfortunatly don't have much time right now).

 

 

 

Cheers,

Anzeo

Link to comment
Share on other sites

OK I think I almost got it :)

 

But there's one problem and I think I knoiw what's causing it it, but I can't figure out how to adjust the query to solve it.

The problem is the output is not returning topics with no posts (so no replies). I tried right or left join but nothing changed.

 

EDIT: Hmm, apparently for some strange reason it's not updating correctly. At first it seemed working but the order of the topics does not change when I add a reply.

Link to comment
Share on other sites

Okay after taking a look at it, I still haven't figured out yet what's wrong, I did exactly as you said, but the topics are ordered by a specific order and won't be rearranged again.

 

Can you help me out with this last bit, it's nearly done :)

Link to comment
Share on other sites

Ok I switched my table structures and everything is still working great except for two consistent problems.

 

My query:

 

$tpcqry = "SELECT * FROM ( SELECT TOPIC.ID AS TID, TOPIC.Datum AS TDatum, POST.Datum AS PDatum, TOPIC.Titel AS Titel, TOPIC.Forum AS Forum, TOPIC.Auteur AS TAuteur FROM TOPIC INNER JOIN POST ON TOPIC.ID = POST.Topic GROUP BY TOPIC.ID HAVING (MAX(POST.Datum))) AS TOPICS WHERE Forum='$FID' ORDER BY TOPICS.PDatum DESC";

 

Datum column format "0000-00-00 00:00:00".

 

The problems I get while outputting:

 

1. When a topic hasn't been replied to, it does not show up in the list.

2. It's still not ordering correctly :/

 

Do you have a clue at what I'm doing wrong here?

 

I cannot repeat "thank you" enough :)

Link to comment
Share on other sites

"SELECT * FROM ( SELECT TOPIC.ID AS TID, TOPIC.Datum AS TDatum, POST.Datum AS PDatum, TOPIC.Titel AS Titel, TOPIC.Forum AS Forum, TOPIC.Auteur AS TAuteur FROM TOPIC INNER JOIN POST ON TOPIC.ID = POST.Topic GROUP BY TOPIC.ID HAVING (MAX(POST.Datum)) ORDER BY COLUMN HERE) AS TOPICS WHERE Forum='$FID' ORDER BY TOPICS.PDatum DESC";

 

Note the "ORDER BY COLUMN HERE" portion. Dunno if it will work but give it a try.

Link to comment
Share on other sites

This should do it.

 

SELECT * FROM (
SELECT TOPIC.ID AS TID,
  TOPIC.Datum AS TDatum,
  POST.Datum AS PDatum,
  MAX( IFNULL(POST.Datum, TOPIC.Datum)) as orderByDatum,
  TOPIC.Titel AS Titel,
  TOPIC.Forum AS Forum,
  TOPIC.Auteur AS TAuteur
FROM TOPIC
LEFT JOIN POST ON (TOPIC.ID = POST.Topic)
GROUP BY TOPIC.ID HAVING (MAX(TO_DAYS(IFNULL(POST.Datum, TOPIC.Datum)))))
AS TOPICS
ORDER BY TOPICS.orderByDatum DESC

 

Good luck,

 

Patirck

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.