Tag Archives: mysql

Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated

SQL92 and earlier versions do not allow the selection of lists, HAVING condition or ORDER BY list references unnamed non-grouped columns in the GROUP BY clause. For example, in standard SQL92, this query is illegal because columns that are not listed in the name selection list do not appear in the GROUP BY :

 
    SELECT o.custid, c.name, MAX(o.payment) FROM orders AS o, customers AS c WHERE o.custid = c.custid GROUP BY o.custid;

For the query in SQL92 to be valid, the name column must be omitted from the selection list or named in the GROUP BY clause.
SQL99 later allows for every optional function, T301, and so nonaggregates, if they are funcally dependent on GROUP BY column: if there is such a relationship between name, and custid, the query is legal. This is the case, for example, is custid the main key customers.
MySQL 5.7.5 or above function dependency detection function. If ONLY_FULL_GROUP_BY is enabled for SQL mode (BY default), MySQL rejects the list of options, HAVING conditions or the ORDER BY list references a subset that neither specifies the GROUP BY non-collection column, nor is it functionally dependent on them. (prior to 5.7.5, MySQL did not detect functional dependency, ONLY_FULL_GROUP_BY is not enabled by default). For a description of the behavior prior to 5.7.5, see the MySQL 5.6 Reference Manual.)
If ONLY_FULL_GROUP_BY is disabled, then the MySQL extension used BY standard SQL GROUP BY allows the selection of lists, HAVING conditions, or ORDER BY lists reference non-collection columns, even though the columns do not functionally depend on the GROUP BY columns. This causes MySQL to accept the previous query. In this case, the server is free to select any value in each group, so unless they are the same, the selected value is uncertain, which may not be what you want. In addition, the choice of the value for each group cannot be influenced BY adding a ORDER BY clause. The result set sorting occurs after the selected value, ORDER BY does not affect which value in each group the server selects. Disabling ONLY_FULL_GROUP_BY is useful primarily if you know that due to some attribute of the data each unnamed GROUP BY has the same value for each GROUP in each non-grouping column.
You can achieve the same effect without ONLY_FULL_GROUP_BY by referring to a non-grouped column using ANY_VALUE().
The following discussion illustrates functional dependencies, error messages when MySQL’s functions are not dependent, and the methods that cause MySQL to accept queries without functional dependencies.
This query may be ONLY_FULL_GROUP_BY is disabled because the column not listed in the address select list is not named in the GROUP BY clause:

SELECT name, address, MAX(age) FROM t GROUP BY name;

This query is valid if name is the primary key t or the only NOT NULL column. In this case, MySQL recognizes that the selected column is functionally dependent on the grouping column. For example, if name is the primary key, then the value is determined, address because each group has only one value of the primary key, so there is only one row. Therefore, there is no randomness in the selection of values in the address group, and there is no need to reject the query.
If name is NOT the primary key t or the only NOT NULL column, the query is invalid. In this case, functional relevance cannot be inferred and an error occurs:

 
    mysql> SELECT name, address, MAX(age) FROM t GROUP BY name; ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mydb.t.address' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

If you know, for a given data set, every name value actually uniquely identifies this address value, this address is functionally dependent name. To tell MySQL to accept a query, use the following ANY_VALUE() function:

SELECT name, ANY_VALUE(address), MAX(age) FROM t GROUP BY name;

Or, disable ONLY_FULL_GROUP_BY.
However, the above example is simple. In particular, you cannot group on a single primary key column because each group contains only one row. For additional examples of functional dependencies in more complex queries, see Section 12.19.4, “Functional dependency Detection.”
If the query has an aggregation function and no GROUP BY clause, it cannot have a non-collection column in the select list, HAVING condition, or the ORDER BY list ONLY_FULL_GROUP_BY :

 
    mysql> SELECT name, MAX(age) FROM t; ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'mydb.t.name'; this is incompatible with sql_mode=only_full_group_by

There is no GROUP BY, there is a separate GROUP, and it is uncertain which name value is selected for the GROUP. Here also ANY_VALUE() can be used, if it is irrelevant, nameMySQL selects which value:

SELECT ANY_VALUE(name), MAX(age) FROM t;

ONLY_FULL_GROUP_BY also affects the handling of ORDER BY using a DISTINCT and queries in MySQL 5.7.5 or later. In the case of the table, t has three columns c1, c2 and c3 contain these rows:

c1 c2 c3
1  2  A
3  4  B
1  2  C

Suppose we execute the following query and expect the results to be sorted in the following order c3 :

SELECT DISTINCT c1, c2 FROM t ORDER BY c3;

To order results, you must first repeat. But to do that, should we keep the first row or the third row?This arbitrary choice affects the reserved value c3, which in turn affects the sorting and makes it arbitrary. To prevent this problem, if any expression does not meet at least one of the following criteria, the query with DISTINCT and ORDER BY is rejected is invalid ORDER BY :
The expression equals that all columns of the selected table referenced by an expression in the selection list and belonging to the query are elements of the selection list
Another MySQL extension of standard SQL allows alias expressions to be referenced in the HAVING clause in the select list. For example, the following query returns name values that occur only once in the table orders :

 
    SELECT name, COUNT(name) FROM orders GROUP BY name HAVING COUNT(name) = 1;

MySQL extension allows HAVING to use aliases in clauses of aggregated columns:

 
    SELECT name, COUNT(name) AS c FROM orders GROUP BY name HAVING c = 1;

Pay attention to
Prior to MySQL 5.7.5, enabling ONLY_FULL_GROUP_BY disables this extension, so you need HAVING to write this clause using an unused expression.

Standard SQL only allows column expressions in the GROUP BY clause, so such a statement is invalid because it FLOOR(value/100) is a non-column expression:

 
    SELECT id, FLOOR(value/100) FROM tbl_name GROUP BY id, FLOOR(value/100);

MySQL extends standard SQL to allow non-column expressions in the GROUP BY clause and treats the above statement as valid.
Standard SQL also does not allow aliases in the GROUP BY clause. MySQL extends standard SQL to allow aliases, so another way to write queries is as follows:

 
    SELECT id, FLOOR(value/100) AS val FROM tbl_name GROUP BY id, val;

The alias val in this clause is treated as the column expression GROUP BY.
When a non-column expression exists in the GROUP BY clause, MySQL recognizes the equality between that expression and the expression in the selection list. This means that when ONLY_FULL_GROUP_BY is enabled for the QL schema, the contained query GROUP BY id, FLOOR(value/100) is valid, because FLOOR() appears in the selection list with the same expression. MySQL, however, does not attempt to identify functional dependencies on non-GROUP BY column expression, so ONLY_FULL_GROUP_BY, even though the third selected expression is a simple formula for id column and FLOOR() expression in the clause, the following query is invalid GROUP BY :

 
    SELECT id, FLOOR(value/100), id+FLOOR(value/100) FROM tbl_name GROUP BY id, FLOOR(value/100);

The solution is to use derived tables:

 
    SELECT id, F, id+F FROM 0 (SELECT id, FLOOR(value/100) AS F1 2 3 4 FROM tbl_name5 6 7 8 GROUP BY id, FLOOR(value/100)) AS dt;9 0

1

Solution to MySQL workbench error 1148 unable to load local data

Windows10 workbench 8.0 error code:1148 temporary solution is as follows :
1. In the Workbench, type Show Global variables like ‘local_infile’;
result should be :on. This problem has nothing to do with whether localinfile =1, which is a bug of workbench. 2. Continue to enter show variables like ‘secure_file_priv’;
the result should be C:\ProgramData\MySQL\MySQL Server 5.7\Uploads file; 3. Put the files that need to be imported into the file in step 2;
4. Load data local infile command to remove the local, and combined with step 2 folder path, for example, is as follows:
the load data infile ‘C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/a.c sv into table a’;
Ps: resources: https://bugs.mysql.com/bug.php?Id =91891 Post Catherine S reply, like one!

Solution to error 1452: cannot add or update a child row: a foreign key constraint failures in MySQL

When executing a SQL query, I suddenly reported 1452, which made me totally confused.
In fact, the main reason for the error of 1452 is that there is no data in the main table corresponding to the child table with foreign keys.
For example:
Class list:
The

id

name

1

advanced classes

2

regular class
Student table:

id

name

class_id

1

* *

1

2

and

2

3

fifty

3
As a result, there is no data for the class with class_ID 3 for king 5 in the table.
So no matter how you do the update statement on king five he’s going to report an error.
 
The real reason:
 
I copied a “class list” from the official library, and then I copied a “student list”, and then I made a mistake in the operation of the student list.
Because after the class table was copied and before the student table was copied, new classes and students were added on the line. Then the data I copied here was only students, and no class was finished…
It’s embarrassing.
So don’t mess around…
 
Solution 1:
Clear both tables and redo them.
Solution 2:
Use SQL to delete data that has no data in the main table corresponding to the foreign key.
Take the above example: Delete Wang Wu.

MySQL error: can’t create table ‘..’( errno:150 )Solutions

scenario
Student table S (SNO, SNAME, SAGE), class schedule C (CNO, CNAME)
When the course selection table (SC) is created, set (SNO, CNO) as the primary key and SNO and CNO as the foreign keys

drop table if exists sc1;
create table sc1(
    sno varchar(10),
    cno varchar(10),
    grade numeric,
    primary key(sno,cno),
    foreign key(sno) references s(sno),
    foreign key(cno) references c(cno)
);

Error warning:

The solution
1. Check whether the types and sizes of foreign key fields in SC table are exactly the same as those in S table C table
2. One of the foreign keys you are trying to reference has no index or is not a primary key. If one of the foreign keys is not a primary key, you must create an index for it.
3, one or two tables are MyISAM engine table, if you want to use foreign key constraints, must be InnoDB engine

The error of the author is that cNO is not set as primary key in Table C, which can be solved by setting it once

mysql error 1093

Error Code: 1093. You can’t specify target table ‘t_user’ for update in FROM clause 0.015 SEC
data cannot be query in the same table as update data in the same table.
note that this problem occurs only in mysql; MSSQL and oracle do not.
Keyword search: mysql Error 1093
Example:


delete from t_user where id in (select id from t_user where id > 26);
Error Code: 1093. You can’t specify target table ‘t_user’ for update in FROM clause 0.000 SEC

Solution: select the result of the select through the intermediate table again, so as to avoid the error
change the SQL statement to:

delete from t_user where id in (select * from (select id from t_user where id > 26) tmp);

Then optimize it:

delete t from t_user t join (select id from t_user where id > 22) tmp on tmp.id = t.id;

query data in the same table as the same table update or delete delete, the SQL statement is as follows:
select * from (clause temp)

You can’t specify target table ‘car’ for update in from clause

Error Code: 1093 occurs when the following SQL statement is executed:

update car set tag = 1 where id in (select id from car where brand_id=182 and tag=0);

The reason for the error is that the modified table and the queried table are the same table, which is not allowed in MySQL. We can solve this problem by querying again in the middle:

update car set tag = 1 where id in (select id from (select id from car where brand_id=182 and tag=0) As temp);

Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and co

MySQL5.7 Group BY statement error:
Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘t.road_name’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
The reason I looked for it was because in versions prior to MySQL5.7,
MySQL does not detect functional dependencies,
The only_FULL_group_BY default is not enabled.
Only_full_group_by default is enabled in MYSQL5.7.
So MySQL will reject queries that reference a list, condition, or order,
These queries refer to unnamed non-aggregated columns in a group rather than relying on them functionally.
Solutions:
Find the MySQL configuration file
Find /etc/my.cnf on the server and edit it

Add a sentence to the document:
sql_mode=’STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’

Then restart MySQL. LNMP is installed here. Just enter LNMP restart

ERROR 2005 (HY000): Unknown MySQL server host

One: Problem description
[root @ localhost mytop 1.6] #/usr/local/webserver/mysql/bin/mysql -u admin – p12345678 – h 172.29.141.112:3306 – e “show databases;”
ERROR 2005 (HY000): Unknown MySQL server host ‘172.29.141.112:3306’ (0)
[root@localhost mytop-1.6]#
 
Two: Problem solving
[root @ localhost mytop 1.6] #/usr/local/webserver/mysql/bin/mysql -u admin – p12345678 172.29.141.112 – P – 3306 – h e “show databases;”
+ — — — — — — — — — — — — — — — — — — — — +
| Database |
+ — — — — — — — — — — — — — — — — — — — — +
| information_schema |
| | blog
| mysql |
| performance_schema |
| Test |
+ — — — — — — — — — — — — — — — — — — — — +
[root @ localhost mytop 1.6] #
 
Three: The problem
Mysql didn’t support mysql-H IP :port. You might have thought something was wrong, but if you thought about it at the time, you would have found it. You would have posted it on the iptub.
 
Four: Other reasons
Firewall issues, /etc/init.d/iptables stop off, or reset

About MySQL error: subquery returns more than 1 row

Report: SQLSTATE[21000]: Cardinality cheesecake: 1242 Subquery returns more than 1 row
error means that the sub-query results are less than one row. The error is as follows:

Select * from table1 where table1. Colums =(select columns from table2); Take this SQL statement as an example.

1) if you are writing duplicates, remove the duplicates. When writing, you can add a logical decision (PHP) or a foreign key (mysql) to prevent the data from being written repeatedly.
(in my actual development, I encountered the situation of repeatedly writing data, I found the same two data in the database, which is not consistent with the original business requirements)
2) add limit 1 in the sub-query condition statement, find a qualified one can be
select * from table1 where table1. Colums =(select columns from table2 limit 1);

C ා connect mysql: fatal error encountered during command execution solution

When a parameterized query is used to pass a reference to mysql, it is always reported
Fatal error encountered during command execution
Make along while, discovery code is no problem, finally Baidu broad net friend
Solution:
Allow User Variables=True in the connection string
server=; port=3306; user=root; password=1111; database=shujukuming; CharSet=gb2312; Allow User Variables=True;”