» 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 08-02-2006, 05:32 PM
annan annan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
annan is on a distinguished road
Unhappy count() result query

Hello frendz


I'm with version 4.0.27. My query is suppose to display 11 records, but when I join two tables, the result is much more than it supposed to be.

Code:
SELECT t1.f1, t2.f1, count( t1.f1 ) AS totalc
FROM USA
LEFT JOIN CANADA ON t1.f1 = t2.f1
WHERE t1.f2 = '1'
GROUP BY t1.f1
ORDER BY 'totalc' DESC

Thanks in advance for your replies -- 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 08-02-2006, 05:34 PM
tyrisha tyrisha is offline
Junior Member
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
tyrisha is on a distinguished road
Default

You need to group by all non-aggregate fields, or else you'll get unpredictable results:
code;

SELECT
t1.f1
, t2.f1
, COUNT( t1.f1 ) AS totalc
FROM USA
LEFT JOIN CANADA ON t1.f1 = t2.f1
WHERE t1.f2 = '1'
GROUP BY
t1.f1
, t2.f1
ORDER BY
totalc
DESC

SELECT t1.f1, t2.f1, COUNT( t1.f1 ) AS totalcFROM USA LEFT JOIN CANADA ON t1.f1 = t2.f1 WHERE t1.f2 = '1'GROUP BY t1.f1 , t2.f1 ORDER BY totalc DESC -- 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
  #3  
Old 08-02-2006, 05:37 PM
algo algo is offline
OSP Starters
 
Join Date: Jul 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
algo is on a distinguished road
Smile

What tyrisha said is fine, you should always use correct GROUP BY syntax

however, in this case it will not solve the problem because t1.f1 = t2.f1

also, your original query as given won't run, because t1 and t2 are not defined

assuming that USA is t1 and CANADA is t2, try this --
Code:
SELECT t1.f1
, t2.f1
, ( select count(*)
from USA
where f1 = '1' ) AS totalc
FROM USA as t1
LEFT
JOIN CANADA as t2
ON t2.f1 = t1.f1
WHERE t1.f2 = '1'
ORDER
BY totalc DESCnote also that the ORDER BY is a column, not a string

cheers -- 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
Mysql_affected_rows() isn't working on a delete query? zizu PHP Programming and Tips 1 08-26-2006 03:05 PM
How does Overture count relate to Google search count? robotec Google 6 08-15-2006 06:53 PM
Intersect - SQL query help shakiel MySQL Forums 1 08-15-2006 03:16 PM
Query Browser issue..... Riyan MySQL Forums 1 08-04-2006 11:38 AM
Query works on production but not on testing server parore MySQL Forums 1 07-31-2006 04:11 PM


All times are GMT. The time now is 09:53 AM.