Fedora Linux Support Community & Resources Center
  #1  
Old 20th August 2011, 04:35 PM
roelj Offline
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Age: 21
Posts: 279
linuxsafari
MySQL query problem

This is the query I'm running:

Code:
SELECT `user_id`, SUM(`is_correct`) / COUNT(`id`) AS `score` FROM `UserAnswers` 
WHERE `user_id` IN ( SELECT DISTINCT `user_id` FROM `UserAnswers` );
Which gives this result:
Code:
+---------+--------+
| user_id | score  |
+---------+--------+
|       1 | 0.7647 |
+---------+--------+
The subquery returns:
Code:
+---------+
| user_id |
+---------+
|       1 |
|       4 |
+---------+
So, why does the first query only return 1 row, while the subquery returns 2 rows?

Thanks for your time.
Reply With Quote
  #2  
Old 20th August 2011, 06:32 PM
andinowich Offline
Registered User
 
Join Date: Jul 2011
Posts: 3
linuxfirefox
Re: MySQL query problem

Hi roelj,

I think the problem is that you use group-functions like "sum" and "count". Try the following query:

Code:
SELECT `user_id`, SUM(`is_correct`) / COUNT(`id`) AS `score` FROM `UserAnswers` GROUP BY `user_id`
regards
andinowich
Reply With Quote
  #3  
Old 20th August 2011, 07:00 PM
roelj Offline
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Age: 21
Posts: 279
linuxsafari
Re: MySQL query problem

That works! Great solution!

However, I don't understand how the SUM() and/or COUNT() function(s) mess it up.
The functions are used on a row result, so it should only affect one row..?

Thanks for the quick reply and solution. It's working correctly now.
Reply With Quote
  #4  
Old 21st August 2011, 08:01 AM
andinowich Offline
Registered User
 
Join Date: Jul 2011
Posts: 3
linuxfirefox
Re: MySQL query problem

Hi!

The SUM and COUNT functions sum up and count all rows which are returned by the query if you don't use group by. Maybe the following link will help you:

http://dev.mysql.com/doc/refman/5.0/...functions.html

I'm new in this forum but I think you should mark the thread as solved?

regards
andinowich
Reply With Quote
  #5  
Old 22nd August 2011, 12:55 PM
roelj Offline
Registered User
 
Join Date: Jun 2009
Location: Netherlands
Age: 21
Posts: 279
linuxsafari
Re: MySQL query problem

OK, thanks for the solution and the explanation :-).
Reply With Quote
Reply

Tags
mysql, problem, query

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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 query browser mlocicero Using Fedora 2 16th January 2011 08:23 PM
Help with MySQL Query Jamwa Programming & Packaging 3 2nd August 2010 09:53 PM
mysql query browser jet Servers & Networking 0 22nd March 2006 10:49 PM
FC3 and mysql administrator and mysql query browser elmargaro Using Fedora 0 7th November 2005 06:01 PM


Current GMT-time: 22:16 (Tuesday, 18-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat