WordPoints / top-users-in-period

Display the top points earners within a given period of time
GNU General Public License v2.0
0 stars 0 forks source link

Database error when counting block logs #17

Closed JDGrimes closed 7 years ago

JDGrimes commented 7 years ago

The block logs query does not override the count() method to give any kind of warning like the main query does, so it would seem that the count() method is intended to be usable. However, attempting to use it will produce a database error:

WordPress Database Error: Unknown column 'total' in 'order clause' [SELECT COUNT(*)
FROM `wptests_wordpoints_top_users_in_period_block_logs`
WHERE `user_id` = 5
GROUP BY `user_id`

ORDER BY `total` DESC
]

We need to prevent this by either giving a higher-level error and essentially disabling the method, or making the method work.

The issue is the ORDER BY clause, which references the total column. It isn't a column at all, but the sum of the points column, which we aren't calculating when we are just counting.

Probably the order clause can be omitted here, since it seems irrelevant to counting. Perhaps that should even be pulled upstream so that the order clause is always omitted from count queries. But regardless we'll need to fix this here in some way.

JDGrimes commented 7 years ago

The GROUP BY clause also has to be omitted, and COUNT(DISTINCT) used.

JDGrimes commented 7 years ago

Our points logs query suffers from the same issue.