Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
|
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2007, 12:02 PM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL help requested

L.S.

Could anyone help me with my SQL query? The tables I need for my query:

tblUser
-------
u_id
u_nickname
[...]

tblTopicComment
---------------
tc_id
tc_topicId
tc_userId
tc_contentbody
tc_insertdate
[...]

I want to select:
a) the most recent comments on a specific topic AND
b) from different users

I now use:

SELECT tc_contentbody, UNIX_TIMESTAMP(tc_insertdate), u_nickname FROM tblTopicComment INNER JOIN tblUser on (tc_userId = u_id)
GROUP BY tc_userId
ORDER BY tc_insertdate DESC LIMIT 3

This query doesn't seem to grab the most recent comments. I've also tried using the DISTINCT, but that didn't work for me either.

Any suggestions?


Thanks in advance,
MvO


 
Old March 17th, 2007, 12:55 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this:
SELECT tc_contentbody, tc_insertdate, u_nickname
FROM tblUser, tblTopicComment
WHERE u_id = tc_userId
ORDER BY tc_insertdate DESC
LIMIT 3;


 
Old March 17th, 2007, 04:46 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi jmaronilla,

Thanks for your comment. But with your solution I wouldn't retrieve the most recent comments "from different users". I've found the following solution

SELECT tblTopicComment.tc_contentbody, tblTopicComment.tc_insertdate, tblUser.nickname
FROM tblTopicComment
LEFT JOIN tblUserON ( tblTopicComment.tc_userId = tblUser.u_id )
WHERE tblTopicComment.tc_id
IN (
SELECT MAX( tblTopicComment.id ) AS last_comment
FROM tblTopicComment
GROUP BY tc_userId
ORDER BY last_review DESC
)
ORDER BY tc_insertdate DESC

Solved! Thanks.

 
Old March 17th, 2007, 09:19 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
Default

Your solution with field last_review was not originally on your field list for your criteria that is why I did not know. Just thought my sql syntax will make it simplier. Anyway you got your solution to your problem. Cheers!!!

 
Old March 18th, 2007, 03:38 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for the misunderstanding and thanks a lot for your comment!






Similar Threads
Thread Thread Starter Forum Replies Last Post
The report you requested requires further informat BreathShadow ASP.NET 2.0 Basics 0 April 13th, 2007 01:56 PM
Redirect to the requested page KamalRaturi ASP.NET 2.0 Professional 1 September 27th, 2006 04:32 AM
The webpage you requested is not available offline cathiec ASP.NET 1.0 and 1.1 Basics 0 October 6th, 2005 09:35 AM
Order by more a requested variable bleutiger Classic ASP Databases 3 March 1st, 2005 12:19 PM
Tool recommendation requested pankaj_daga Oracle 1 July 11th, 2004 09:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.