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

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-04-2009, 07:36 PM   PM User | #1
MattNolan
New Coder

 
Join Date: Sep 2009
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
MattNolan is an unknown quantity at this point
Need data from 2 columns in 1 query

Edit: Should say 2 tables in 1 query

I need to select:

select * from blogs;

and I need to pair this with

select count(*) from comments group by aid;

where aid is in both blogs as the main key, and aid is just a regular field in the comments table

Last edited by MattNolan; 11-04-2009 at 07:44 PM..
MattNolan is offline   Reply With Quote
Old 11-04-2009, 07:53 PM   PM User | #2
Fumigator
Master Coder

 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 5,411
Thanks: 32
Thanked 373 Times in 364 Posts
Fumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really nice
The way to do this is to take the simple COUNT(*) query, grouping by the id:

Code:
SELECT COUNT(*), table_id
FROM table2
GROUP BY by table_id
And make that a TEMP table, joined with the main table.

Code:
SELECT * FROM main_table as m
JOIN (
    SELECT COUNT(*), main_table_id
    FROM table2
    GROUP BY main_table_id) as count_table
ON m.table_id = count_table.main_id
__________________
Fumigator is offline   Reply With Quote
Old 11-04-2009, 09:02 PM   PM User | #3
Old Pedant
Senior Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 660 Times in 652 Posts
Old Pedant will become famous soon enoughOld Pedant will become famous soon enough
Why the inner SELECT???

Why not a simple JOIN??

Code:
SELECT B.field1, B.field2, B.field3, COUNT(c.aid) AS howManyComments
FROM blogs AS B INNER JOIN comments AS C
ON B.aid = C.aid
GROUP BY B.field1, B.field2, B.field3
Now, if any blogs record does *NOT* have any comments as of yet, then that blogs records will not appear at all.

If you want all blogs records to appear, even if the count of comments is zero, then just change the INNER JOIN to a LEFT JOIN.
__________________
"Old age and cunning win out over youth and enthusiasm every time."
Old Pedant is offline   Reply With Quote
Old 11-04-2009, 10:31 PM   PM User | #4
Fumigator
Master Coder

 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 5,411
Thanks: 32
Thanked 373 Times in 364 Posts
Fumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really niceFumigator is just really nice
To avoid grouping by every selected column.
__________________
Fumigator is offline   Reply With Quote
Old 11-04-2009, 11:25 PM   PM User | #5
Old Pedant
Senior Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 660 Times in 652 Posts
Old Pedant will become famous soon enoughOld Pedant will become famous soon enough
Quote:
To avoid grouping by every selected column.
Hmmm...I'd actually think that *forcing* him to select the individual columns and then group by them might be a good idea.

But you make a point worth considering, for sure.

My gut feel is that you won't see a performance difference between the two, but maybe I'm wrong. Will have to test that out one of these days.
__________________
"Old age and cunning win out over youth and enthusiasm every time."
Old Pedant is offline   Reply With Quote
Old 11-05-2009, 01:24 PM   PM User | #6
MattNolan
New Coder

 
Join Date: Sep 2009
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
MattNolan is an unknown quantity at this point
Thanks Guys

Great solutions guys, definitely helped me, I was completely unsure of the join syntax and this is perfect.
MattNolan 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 05:10 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.