MySQL errors in Dashboard

Bug #173268 reported by JW
2
Affects Status Importance Assigned to Milestone
Chameleon (inactive)
Fix Released
Medium
JW

Bug Description

In Admin::DashboardController is written:
    @most_active_commenter = Comment.find(:first, {:select => "author, website",
      :order => "COUNT(author) DESC", :conditions => "approved = 1"})
this executes the SQL:
    SELECT author, website FROM comments WHERE (approved = 1) ORDER BY COUNT(author) DESC LIMIT 1
which returns the error:
    Invalid use of group function
This happens because we're using an aggregate function (COUNT) in the ORDER BY clause.
More info about this message is, amongst others, at http://sqlzoo.net/howto/source/z.dir/err934/mysql and http://www.thescripts.com/forum/thread691615.html.

An example of this bug is on http://chameleon.wikidot.com/forum/t-28692/production-problems-with-dashboard-and-comments#post-74110, the first one.

Revision history for this message
JW (jw-00000) wrote :

A possible solution is the query
    SELECT author, website, COUNT(author) FROM comments WHERE approved = 1
    GROUP BY author ORDER BY COUNT(author) DESC LIMIT 1

Changed in chameleon:
assignee: nobody → jw-00000
status: New → In Progress
Revision history for this message
JW (jw-00000) wrote :

The first solution doesn't work, this will:
    SELECT author, website, COUNT(author) as count FROM comments
    WHERE approved = 1 GROUP BY author ORDER BY count DESC LIMIT 1
This is the same as the first, only now we're naming COUNT(author) 'count',

Changed in chameleon:
importance: Undecided → Medium
milestone: none → 0.6.2
Revision history for this message
JW (jw-00000) wrote :

This same problem happens with the code:
    @most_used_tags = Tag.find_by_sql "SELECT name, COUNT(*) AS count " +
      "FROM tags JOIN entries_tags ON tags.id = entries_tags.tag_id " +
      "GROUP BY name ORDER BY count DESC LIMIT 5"
in Admin::DashboardController#welcome.
Changing the SQL to this should work:
    SELECT name, COUNT(*) AS count FROM tags JOIN entries_tags ON tags.id = entries_tags.tag_id
    GROUP BY name ORDER BY count DESC LIMIT 5

Revision history for this message
JW (jw-00000) wrote :

Fixed in trunk.

Changed in chameleon:
status: In Progress → Fix Committed
JW (jw-00000)
Changed in chameleon:
milestone: 0.6.2 → 0.6.1.2
JW (jw-00000)
Changed in chameleon:
milestone: 0.6.1.2 → 0.6.2
JW (jw-00000)
Changed in chameleon:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.