Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-06-2012, 03:12 AM   PM User | #1
Chris Hick
Regular Coder

 
Join Date: Oct 2010
Location: Florence, MS
Posts: 476
Thanks: 10
Thanked 33 Times in 32 Posts
Chris Hick is an unknown quantity at this point
Correct syntax for getting information from two tables

I know this is possible and I've done it quite often in the past. But I haven't used MySQL in months(been doing some C++ work. Not that it has anything to do with C++.) Anyhow, what I have is two table Posts and Comments and I'm trying to get all the posts and then count the number of comments those posts have. I do not need to get any information from the comments table other than counting how many comments there are to a single post. I want to do this with mysql only.
__________________
Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
I always recommend the HEAD First series of books for learning a new coding language. ^_^
Chris Hick is offline   Reply With Quote
Old 09-06-2012, 09:51 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Please post the relevant column names of your tables.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 09-07-2012, 01:12 AM   PM User | #3
Chris Hick
Regular Coder

 
Join Date: Oct 2010
Location: Florence, MS
Posts: 476
Thanks: 10
Thanked 33 Times in 32 Posts
Chris Hick is an unknown quantity at this point
Sorry, I am just now getting on. I have figured out how to do it and return the result that I like. The only issue I have is that it always returns information even when there is no data to be returned. Btw, I am using codeigniter framework at the moment. Here is what I used:
Code:
$this->db->select("posts.*, COUNT(comments.id) AS numberOfComments", FALSE); 
$this->db->where('posts.type_id',$type);
$this->db->from('posts');
$this->db->join('comments', 'comments.post_id = posts.id', 'left');
__________________
Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
I always recommend the HEAD First series of books for learning a new coding language. ^_^
Chris Hick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:01 AM.


Advertisement
Log in to turn off these ads.