MySQL joint table query deduplication & resolve DISTINCT exception: Expression #1 of ORDER BY clause is not in SELECT list, references...

    When performing joint queries on multiple tables in a MySQL database, duplicate values ​​are often encountered, requiring deduplication. Here are several methods to remove duplicates.

1. MySQL joint table query deduplication

1. Use the distinct keyword

SELECT DISTINCT *
FROM table1
JOIN table2
ON table1.id = table2.id;

2. Use GROUP BY

SELECT *
FROM table1
JOIN table2
ON table1.id = table2.id
GROUP BY table1.id;

3. Use subqueries

SELECT *
FROM (SELECT *
FROM table1
GROUP BY id) sub1
JOIN (SELECT *
FROM table2
GROUP BY id) sub2
ON sub1.id = sub2.id;

2. Solve the problem of exceptions thrown when using DISTINCT with ORDER BY

    When DISTINCT is used in conjunction with ORDER , the field used for sorting ( order by sort_num ) must be in the select list, otherwise a MySQL exception prompt similar to the following will be thrown:

Caused by: java.sql.SQLException: Expression #1 of ORDER BY clause is not in SELECT list, references column 'xxx.x.sort_num' which is not in SELECT list; this is incompatible with DISTINCT

Solution steps:

1. Check whether the ONLY_FULL_GROUP_BY rule verification is turned on

SELECT @@GLOBAL.sql_mode;
// 查询结果:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

SELECT @@SESSION.sql_mode;
// 查询结果:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

2. Turn off ONLY_FULL_GROUP_BY rule verification

// 去掉“ONLY_FULL_GROUP_BY,”后的值
set @@GLOBAL.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

set @@SESSION.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

3. Restart the MySQL server

# 该令如执行失败,会提示:Redirecting to /bin/systemctl start mysql.service 
service mysqld start;

# 上面命令如果执行失败,可改用此命令
systemctl start mysqld

Guess you like

Origin blog.csdn.net/crazestone0614/article/details/132767135