Jump to content

just a quick join question


tjhilder

Recommended Posts

Hi,

 

i have this join..

 

$query = "SELECT news.*, COUNT(comment_id) AS count FROM news LEFT JOIN comments ON news.news_id=comments.news_id GROUP BY news_id DESC LIMIT $limitvalue, $limit";

 

at the mo, I have 2 tables, one called news, one called comments, both store the news_id, member_id and username, but instead of storing the username, I want to change it so that it only stores the member_id, so that if I have to change the username of a member, it will change the username on all the posts automaticly by the member_id.

 

I figured it was a case of adding another LEFT JOIN to this query, is this correct? maybe something like this.

 

$query = "SELECT news.*, COUNT(comment_id) AS count FROM news LEFT JOIN comments ON news.news_id=comments.news_id LEFT JOIN members.member_id=comments.member_id GROUP BY news_id DESC LIMIT $limitvalue, $limit";

 

I'm not sure if thats correct, if not maybe someone could help me out :) I'm not sure if it will work, maybe just for the comments, whereas I'd like to do it with the news table as well, not sure if possible.

Link to comment
Share on other sites

You're missing the new table name, and the syntax is wrong -- you've only incuded the ON clause! Try the following (UNTESTED):

 

$query = "SELECT news.*, COUNT(comment_id) AS count FROM news LEFT JOIN comments ON news.news_id=comments.news_id LEFT JOIN members ON members.member_id=comments.member_id GROUP BY news_id DESC LIMIT $limitvalue, $limit";

 

Hope that helps.

Link to comment
Share on other sites

I just realised I mentioned that wrong, was meant to be joining members with news, not members with comments (although I do need that) but the query I gave needs members with news, I have another query for getting comments (should be able to work it all out now tho).

 

I do have one question though, would these 2 bits of code do different things or does it matter which way they are?

 

members.member_id=comments.member_id

comments.member_id=members.member_id

 

thanks :)

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Ok run into a problem with my join.

 

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--]$query = "[span style=\'color:blue;font-weight:bold\']SELECT news2.*, COUNT(comment_id) AS count FROM news2 LEFT JOIN members2 ON news2.member_id=members2.member_id LEFT JOIN comments2 ON news2.news_id=comments2.news_id WHERE news2.news_id={$_GET['[/span]id']} GROUP BY news2.news_id LIMIT 1"; [!--sql2--][/div][!--sql3--]

 

the count is fine, getting news from news2 isn't a problem, just can't seem to get the username from members2

 

what it basicly is doing is, counting the amount of comments from comments2 where news_id is XX (news2 and comments2 have a news_id)

 

it also needs to get the username from members2 where member_id is the same on news2 and members2, got it working with my comments, but can't seem to get it working with this one, not sure why.

 

my working comments query:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--]$view_comments = "[span style=\'color:blue;font-weight:bold\']SELECT * FROM comments2 LEFT JOIN members2 ON members2.member_id=comments2.member_id WHERE news_id={$_GET['[/span]id']} GROUP BY comment_id [span style=\'color:green\']ORDER BY comment_id ASC"[/span]; [!--sql2--][/div][!--sql3--]

 

hope this all makes sense

 

MySQL version 4.1.14-standard

Link to comment
Share on other sites

Ok so I changed it from " SELECT news2.*, " to " SELECT news2.*, members2.*, " and it seems to have worked.

 

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--]$query = "[span style=\'color:blue;font-weight:bold\']SELECT news2.*, members2.*, COUNT(comment_id) AS count FROM news2 LEFT JOIN members2 ON news2.member_id=members2.member_id LEFT JOIN comments2 ON news2.news_id=comments2.news_id WHERE news2.news_id={$_GET['[/span]id']} GROUP BY news2.news_id LIMIT 1"; [!--sql2--][/div][!--sql3--]

 

just gotta make sure it's working all over, if not i'll be back lol

Link to comment
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.