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
https://forums.phpfreaks.com/topic/48310-solved-ordering-a-joined-table/
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?

<?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

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

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

 

 

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";

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.

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 :)

"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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.