Get the App
SLTechnology News&Howtos  ›  Database  › 

Analysis of the problem that Groupby is compatible by using SQL statement incorrectly in Mysql

Shulou Source: shulou.com Published: 2022-05-31 12:41:54 11月13日 Update

The purpose of this article is to share with you the analysis of the problem that Groupby is compatible with the incorrect use of SQL statements in Mysql. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

First, create the database hncu and create the stud table.

Add data:

Create table stud (sno varchar (30) not null primary key,sname varchar (30) not null,age int,saddress varchar (30)); INSERT INTO stud VALUES ('1001mpg); INSERT INTO stud VALUES (' 1002mpg); INSERT INTO stud VALUES ('1003pr' Li Bai', 22nd 'Yiyang'); INSERT INTO stud VALUES ('1004pi' Wang Wu', 24pi 'Beijing, China') INSERT INTO stud VALUES ('1005 Zhangsan', 22 'Yiyang'); INSERT INTO stud VALUES ('1006 Zhangsi', 23 'Yiyang'); INSERT INTO stud VALUES ('1007 Zhangsi', 22 'Hunan Yiyang'); INSERT INTO stud VALUES ('1008 Li Bei, 24 Beijing')

The execution statement is as follows:

VcD4NCjxwcmUgY2xhc3M9 "brush:sql;" > SELECT * FROM stud GROUP BY saddress

The following error is displayed:

ERROR 1055 (42000): Expression # 1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hncu.stud.sno' which is not functionally dependenton columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Then execute this sentence:

Average age of SELECT saddress as FROM stud GROUP BY saddress

-No problem.

Then we use MySQL to execute the wrong code in the previous sentence:

That is:

SELECT * FROM stud GROUP BY saddress

Let's look at the results:

Passed smoothly, however, you found no, the previous smo,sname,age, these three columns of data is not correct ah, yes, MySQL forced to show the first time to find a different saddress line! In fact, this result is wrong, but MySQL should be compatible with this error!

On the other hand, DOS is strictly in accordance with the syntax of SQL.

The grop by syntax for SQL is that select selects the columns in the grouping + aggregate function from table names group by grouped columns

From the perspective of grammatical format, there is a grouping first, and then the retrieved columns are determined, and the retrieved columns can only be selected from the columns that participate in the grouping.

So in the question, the a _ group by _ b _ Q _ c is determined first. It is only after select that a _ can be changed. That is,

The following statements are correct:

Select a from table_name group by c from table_name group by a select a from table_name group by

The following statement is wrong:

Select a from table_name group by c from table_name group by a pm select a cent c from table_name group by a

And because of the power of MySQL, it is compatible with this error!

But not in DOS. So there is a situation where an error is reported under DOS and can be looked up in MySQL (in fact, the result of this search is wrong).

Thank you for reading! On the "Mysql misuse of SQL statements Groupby compatible problem analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: Errors Yiyang sentences questions grouping no data results grammar analysis content more articles Beijing Hunan retrieval different good practical powerful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple NVidia MySQL Linux