» Quick Links
You can make money on these forums
We Share profits with you
Google
Google Adsense
Google Adwords
YPN
Yahoo
MSN Search
Web Directories
Web Hosting
Web Hosting Offers
Hosting News
Suggestions
Link Building
Domain Names
PHP Forums
MySQL Forums
» More Links
OSP News
Reseller Hosting
Shared Hosting
Dedicated Servers
Google Adsense
Search Engine Marketing
Link Development
Affiliate Marketing


» Advertising
Multiple DC PR Check

Free SEO Tools


Go Back   Webmaster Forums > Website Developement / Programming > MySQL Forums

MySQL Forums Share Your Ideas and Tips about MYSQL. Ask questions about MYSQL. Table Design and Much More...

Reply
 
Thread Tools Display Modes
  #1  
Old 05-22-2007, 03:02 AM
wouble wouble is offline
Junior Member
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
wouble is on a distinguished road
Default multi-functions on multi-tables??

Hi all of you,
I wanna write a SELECT that pulls all data from one table, and performs a COUNT() on the other two tables. I can get everything to work fine with the first table and either one of the other two, but on putting two COUNT functions in the same query I get into trouble.

This works great

SELECT mt_forums.*,
COUNT(mt_forum_topics.topic_id) AS forum_topics
FROM mt_forums
JOIN mt_forum_topics ON mt_forums.forum_id=mt_forum_topics.forum_id
GROUP BY mt_forums.forum_id

and of course this works as well

SELECT mt_forums.*,
COUNT(mt_forum_posts.post_id) AS forum_posts
FROM mt_forums
INNER JOIN mt_forum_posts ON mt_forums.forum_id=mt_forum_posts.forum_id
GROUP BY mt_forum_posts.forum_id

but this one seems to multiply the two counts, and return the same value for each alias

SELECT mt_forums.*,
COUNT(mt_forum_topics.topic_id) AS -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Revenue Sharing Ads ( ?):
  #2  
Old 05-22-2007, 07:09 AM
gilbert gilbert is offline
Senior Member
 
Join Date: Dec 2005
Posts: 110
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 22
gilbert is on a distinguished road
Default Re: multi-functions on multi-tables??

Yes, SQL aggregates multiply across multiple joins. So, try to use temp tables or subqueries, such as:

SELECT
f.*,
(SELECT COUNT(t.topic_id) FROM mt_forum_topics t WHERE t.forum_id=f.forum_id),
(SELECT COUNT(f.post_id) FROM mt_forum_posts f WHERE p.forum_id=f.forum_id),
FROM mt_forums f; -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MERGE tables issue! Tyamini MySQL Forums 1 02-09-2007 07:36 AM
deleting 2 tables??? Bkira MySQL Forums 1 01-23-2007 09:21 AM
Lock Tables?? Bailee MySQL Forums 1 08-16-2006 06:54 AM
Getting all rows from two tables cyberhost MySQL Forums 1 07-29-2006 03:17 PM
Host Color Introduces Multi Domain Hosting Plan outsourcingplans Web Hosting Industry 0 04-07-2006 06:37 PM


All times are GMT. The time now is 08:58 PM.