PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 14, 2022

[FIXED] A Database Error Occurred Error Number: 1055 , group by substring

 March 14, 2022     codeigniter, mysql     No comments   

Issue

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shows.b.show_title' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

SELECT b.show_title as show_title FROM `shows` `b` 
WHERE `b`.`active` = 1 
GROUP BY SUBSTRING(show_title, 1, 1)

I'm naming all columns in select query so why the error?

I know the workaround of disabling ONLY_FULL_GROUP_BY from sql_mode but how do i solve the error with the query?


Solution

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


Answered By - spencer7593
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing