Jump to content

Recommended Posts

I'm probably missing something obvious but I cannot find a simple solution to this issue.  I have two tables, users and comments.  In the latter is a column for user id, so I have a query with a LEFT JOIN and the COUNT function to find out how many comments each user has made.  I am also using the MAX function to know when the most recent comment was.  So far so good.  What I would like is to pull out a field from the comments table that corresponds to the latest comment made by each user.

 

Here's the syntax so far

 

SELECT `users`.*, `countries`.*, 
	COUNT(comments.author) AS ct, 
	MAX(comments.submitted) AS last_comment
FROM `users`
LEFT JOIN `countries` ON  `users`.`country_code` = `countries`.`country_code` 
LEFT JOIN `comments` ON `users`.`id` = `comments`.`author`
GROUP BY `users`.`id`
ORDER BY `nickname` ASC

 

As ever any help would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/131901-dynamic-join-syntax/
Share on other sites

you can't get back * -- it's meaningless.
In all my queries * returns all the values in the row.
Once you have the value for the last comment, you need to join back to the comments table
is what I am looking for help with, please.
Link to comment
https://forums.phpfreaks.com/topic/131901-dynamic-join-syntax/#findComment-685428
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.