Q: why the error?
Consider two rows with show_title values of 'True Detective' and 'True Lies'
The expression in the GROUP BY is going to return T
for both of those rows, so those rows are going to be collapsed into a single row in the resultset.
The query can return only a single value for a column in the collapsed row, and it's indeterminate/ambiguous which of the two values to return. With ONLY_FULL_GROUP_BY in sql_mode, MySQL is adhering more closely to the ANSI SQL specification; the 1055 error is behavior similar to what we observe in other relational DBMS e.g. Oracle, Microsoft SQL Server, Teradata, DB2, et al.
Q: how do i solve the error with the query?
The normative pattern is to use an aggregate function to specify which value (out of a set of possible values) to return.
For example, MAX()
or MIN()
. Demonstration:
SELECT SUBSTR(b.show_title,1,1) AS s1
, MAX(b.show_title) AS max_show_title
, MIN(b.show_title) AS min_show_title
FROM shows b
WHERE b.active = 1
GROUP BY SUBSTR(b.show_title,1,1)
will avoid the 1055 error, and could return e.g.
s1 max_show_title min_show_title
-- --------------- ---------------
T True Lies True Detective