The site produces results, but with SELECT COUNT and SELECT query with GROUP BY having two different result counts. This is likely due to the error that is displaying in phpmyadmin but not on the site.
The Queries:
SELECT count(DISTINCT `name`) as `numrows` FROM `users` WHERE `verified` = '1' SELECT `name`, `type`, `language`, `code` FROM `users` WHERE `verified` = '1' GROUP BY `name` ORDER BY `count` DESC LIMIT 0, 25
PhpMyAdmin provides the following error:
1055 - 'main.users.type' isn't in GROUP BY
When reading MySQL docs, I'm still unclear what it is I have to fix. I can't seem to grasp this.
Confusing WHERE and HAVING This statement will return an error because you cannot use aggregate functions in a WHERE clause. WHERE is used with GROUP BY when you want to filter rows before grouping them.
The MySQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.
If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.
Answer: D. GROUP BY clause must contain all the columns appearing in the SELECT statement. It raises error because JOB is not a selected column.
You need to have a full group by:
SELECT `name`, `type`, `language`, `code` FROM `users` WHERE `verified` = '1' GROUP BY `name`, `type`, `language`, `code` ORDER BY `count` DESC LIMIT 0, 25
SQL92 requires that all columns (except aggregates) in the select clause is part of the group by clause. SQL99 loosens this restriction a bit and states that all columns in the select clause must be functionally dependent of the group by clause. MySQL by default allows for partial group by and this may produce non-deterministic answers, example:
create table t (x int, y int); insert into t (x,y) values (1,1),(1,2),(1,3); select x,y from t group by x; +------+------+ | x | y | +------+------+ | 1 | 1 | +------+------+
I.e. a random y is select for the group x. One can prevent this behavior by setting @@sql_mode:
set @@sql_mode='ONLY_FULL_GROUP_BY'; select x,y from t group by x; ERROR 1055 (42000): 'test.t.y' isn't in GROUP BY
The best solution to this problem is, of course, using a complete GROUP BY
expression.
But there's another solution that works around the ONLY_FULL_GROUP_BY
blocking of the old MySQL extension to GROUP BY
.
SELECT name, ANY_VALUE(type) type, ANY_VALUE(language) language, ANY_VALUE(code) code FROM users WHERE verified = '1' GROUP BY name ORDER BY count DESC LIMIT 0, 25
ANY_VALUE()
explicitly declares what used to be implicit in MySQL's incomplete GROUP BY
operations -- that the server can choose, well, any, value to return.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With