Jump to content

Recommended Posts

I have mutliple tables.

The following sql attempts to select all the forums from the forum table.

It also selects all the categories for each selected forum, from the categroies table.

It also selects the most recent post in each selected category from the posts table.

It also selects the user info for the most recent poster.

 

 

$sql = "SELECT

a.*,

b.*,

c.maxPostId,

d.*,

e.*

FROM forums AS a

LEFT JOIN categories AS b ON b.forumId=a.forumId

LEFT JOIN (SELECT categoryId, MAX(postId) AS maxPostId FROM posts) AS c ON c.categoryId=b.categoryId

LEFT JOIN posts AS d ON c.maxPostId=d.postId

LEFT JOIN users AS e ON e.userId=d.userId

";

 

 

problem..

 

The sql works fine except it is only pulling the most recent post regardless of categoryId. It is also only selecting one post in total.

 

I hope you understand.. thanks to any responders.

Link to comment
https://forums.phpfreaks.com/topic/173774-solved-complex-select-thursday-teaser/
Share on other sites

it must be a wanderfull thing to be as brainy as you and the one they call keith.

 

LOL. On the contrary, suicide rates are MUCH higher in the gifted and genius populations. I guess lending some credibility to the phrase "ignorance is bliss." Besides, I'm sure it has more to do with 15 years of experience than braininess.

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.