Jump to content

Recommended Posts

Alright...After a few weeks I was able to figure out a lot of the query.  I have only one thing left.

 

I will re-explain when I am trying to accomplish:

 

I have 3 tables: forums, topics and users.  I need to select all the forums ordered by ID, I also need a topic count for that forum, and the most recent topic with the user information.

 

Currently I get everything including topic but it isnt the most recent.

 

This is what I have:

 

$query = "SELECT `forums`.{$select}

, COUNT(`topics`.`id`) AS `topic_count`

, `topics`.`id` AS `recent_topic_id`

, `topics`.`title` AS `recent_topic_title`

, `topics`.`date` AS `recent_topic_date`

, `users`.`id` AS `recent_topic_poster_id`

, `users`.`username` AS `recent_topic_poster`

  FROM  `forums`

  LEFT

JOIN `topics`

  ON `topics`.`fid`

  = `forums`.`id`

  LEFT

JOIN `users`

  ON `users`.`id`

  = `topics`.`poster`

  WHERE

  `forums`.`cid`='{$id}'

  GROUP BY

  `forums`.`id`

  ORDER BY

  `forums`.`id`";

Link to comment
https://forums.phpfreaks.com/topic/90632-join-help/
Share on other sites

Without working with data i'm got the following for you to try

 

SELECT f.name
      , x.*
      , u.*
FROM forums f
JOIN 
  (SELECT t.fid, COUNT(*) as count
   FROM topics t
   GROUP BY t.fid
  ) tc ON tc.fid = f.id
JOIN
( SELECT * FROM (
    SELECT id,MAX(`date`) as 'max' FROM topics t
    GROUP BY t.id
  ) tm
  JOIN
  (
    SELECT t.fid, t.id, t.title, t.date FROM topics t
  ) ta ON ta.max = tm.date
) x ON x.fid = f.id
JOIN
users u ON u.id = x.poster

Link to comment
https://forums.phpfreaks.com/topic/90632-join-help/#findComment-464945
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.