Jump to content

Complex MySQL HELP!


EKINdesigns

Recommended Posts

I am trying to select from 3 different tables: categories, topics, and users.

 

I need to select all the categories whether or not there are topics in them.  If there are topics i need to get the total amount of topics as well as the most recent one defined my `date`.  I also need to get the users information who posted each topic.

 

This is what I have so far.  Currently if there are no topics in the category it will only return one category when there are actually multiple.  I also can't figure out how to select the most recent topic information.

 

SELECT `forums`.{$select}
    , COUNT(`topics`.`id`) AS `topic_count`
    , MAX(`topics`.`date`) AS `recent_topic_date`
FROM   `forums`
LEFT
JOIN `topics`
  ON `topics`.`fid`
   = `forums`.`id`
LEFT
JOIN `users`
  ON `users`.`id`
   = `topics`.`poster`
WHERE
     `forums`.`cid`='{$id}'
GROUP BY
     `topics`.`fid`

Link to comment
https://forums.phpfreaks.com/topic/87826-complex-mysql-help/
Share on other sites

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.