Jump to content

Dynamic join syntax


virtual_odin

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

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.