tradet Posted September 22, 2008 Share Posted September 22, 2008 Mysql noob calling for assistance here. SELECT DISTINCT comments.blog_id, blog.title FROM comments, blog WHERE comments.blog_id = blog.id ORDER BY comments.date_entered DESC LIMIT 6 Here's my tables comments: id blog_id ip date_entered user_id entry name link blog: id title entry date_entered comments.blog_id points to it's corresponding blog.id ofc. What I want on my site is one "Latest comments" field with the newest comments at the top and links to the blog post where someone left the comment. But with this query I don't get it sorted by date_entered from the comments. If I instead do: SELECT DISTINCT comments.blog_id, comments.date_entered, blog.title FROM comments, blog WHERE comments.blog_id = blog.id ORDER BY comments.date_entered DESC LIMIT 6 I get it sorted but now the blog_id isn't unique. Edit: mysql version 5.xx something Quote Link to comment https://forums.phpfreaks.com/topic/125274-solved-mysql-distinct-join/ Share on other sites More sharing options...
fenway Posted September 29, 2008 Share Posted September 29, 2008 You can't use DISTINCT like that... you have to find the most recent comment first, then join back. Quote Link to comment https://forums.phpfreaks.com/topic/125274-solved-mysql-distinct-join/#findComment-653297 Share on other sites More sharing options...
tradet Posted September 30, 2008 Author Share Posted September 30, 2008 Ehm... Don't know how to Quote Link to comment https://forums.phpfreaks.com/topic/125274-solved-mysql-distinct-join/#findComment-653680 Share on other sites More sharing options...
tradet Posted October 1, 2008 Author Share Posted October 1, 2008 Got it working with SELECT DISTINCT comments.blog_id, blog.title FROM comments JOIN blog WHERE comments.blog_id = blog.id ORDER BY comments.date_entered DESC LIMIT 6 Quote Link to comment https://forums.phpfreaks.com/topic/125274-solved-mysql-distinct-join/#findComment-654660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.