mysql8.0改5.7细节
2021/9/27 2:11:18
本文主要是介绍mysql8.0改5.7细节,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
mysql8.0改5.7细节
先展示演示用表:
一、不接受子查询的 from 后所接的子查询里有主查询字段
这是可以执行的,因为主查询字段a.id没跟在from后
select (select b.val from tab_b b where b.id=a.id) from tab_a a 结果: a bb c
而这种则不行,因为a.id所在的查询语句为from后的子查询
select (select b.val from (select * from tab_b b0 where b0.id=a.id) b) from tab_a a 结果: > 1054 - Unknown column 'a.id' in 'where clause'
所以在做 多个id拼接的字符串转换 (如:多id字符串转中文) 的查询时,可以考虑将主查询的约束条件字段放到子查询的where中:
1 select 2 (SELECT GROUP_CONCAT(b.id) from tab_b b 3 INNER JOIN 4 (SELECT 5 help_topic_id+1 as ID, 6 SUBSTRING_INDEX(SUBSTRING_INDEX(a.val,';',help_topic_id+1),';',-1) AS `value` 7 FROM 8 mysql.help_topic 9 WHERE 10 help_topic_id < LENGTH(a.val)-LENGTH(REPLACE(a.val,';',''))+1) t 11 on b.val = t.`value` and t.`value` != '') 12 from tab_a a where a.id=2 13 14 修改为: 15 16 select 17 (SELECT GROUP_CONCAT(b.id) from tab_b b where FIND_IN_SET(b.val, REPLACE(a.val,';',','))>0 and IFNULL(b.val,'')!='') 18 from tab_a a where a.id=2
注意: 1,当sql语句中用此语句做值转换时,如果有要求顺序,则不可以使用 FIND_IN_SET 方式改写,可以使用代码的方式实现; 2, FIND_IN_SET 的分隔符是 ',' 若字符串使用的分隔符是 ';' 请使用 REPLACE(xxx,';',',') 进行转换;
二、没有row_number()等窗口函数
select row_number() over (ORDER BY a.id desc) as row_number, a.id, a.val from tab_a a 结果: > 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY a.id), a.id, a.val from tab_a a' at line 1 修改为: select @row_number := @row_number+1 as row_number, a.id, a.val from tab_a a, (select @row_number := 0) tt order by a.id desc
注意:是否是分组后再进行计算
如: SELECT row_number() over (PARTITION BY yyy ORDER BY zzz) as max_xxx FROM t 修改为: select t.*, tab.seq FROM t as t INNER JOIN ( select a.id, a.zzz, count(1) as seq from t as a left join t as b on a.yyy=b.yyy and b.sss= 'sss' and a.id>=b.id where a.sss= 'sss' group by a.id,a.yyy ORDER BY a.zzz ) tab on tab.id = t.info_uid WHERE t.sss= 'sss'; (参考分组排序)
上面是排序的2种情况:
1,直接排序, 将结果整体直接排序
2,分组排序, 将结果先按某字段分组,再组内排序
类似的还有 计数、分组计数,最大值、分组最大值 等,改用临时表实现(思路是先分组,再用临时表关联处理):
修改模板:
drop temporary table if exists `temp_list`; create temporary table `temp_list`( ); drop temporary table if exists `temp_group_count`; create temporary table `temp_group_count`( select file_type_id, count(1) as count from temp_list group by file_type_id ); select f.*, c.count from temp_list f inner join temp_group_count c on f.file_type_id = c.file_type_id order by file_type_id desc; drop temporary table if exists `temp_list`; drop temporary table if exists `temp_group_count`;
三、不支持select CAST(1 as float)
select CAST(1 as float) > 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'float)' at line 1 修改为: select CAST(1 as DECIMAL)
四、不接受 insert into xxx(…) (select … union select … union select …)
INSERT into tab_a(id,val) ( select 4,'a' UNION select 5,'b' ) 结果为: > 1064 -You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION select 5,'b' )' at line 4
修改为: INSERT into tab_a(id,val) values(4,'a'),(5,'b')
五、删除语句主表不支持别名,但可以在子查询中引用 表名.字段 做关联
DELETE FROM tab_a a where a.id=1 结果为: > 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a where a.id=1' at line 1 修改为: DELETE FROM tab_a where id=1
欢迎评论指正~
这篇关于mysql8.0改5.7细节的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-04部署MySQL集群项目实战:新手入门教程
- 2024-11-04如何部署MySQL集群资料:新手入门指南
- 2024-11-02MySQL集群项目实战:新手入门指南
- 2024-11-02初学者指南:部署MySQL集群资料
- 2024-11-01部署MySQL集群教程:新手入门指南
- 2024-11-01如何部署MySQL集群:新手入门教程
- 2024-11-01部署MySQL集群学习:新手入门教程
- 2024-11-01部署MySQL集群入门:新手必读指南
- 2024-10-23BinLog入门:新手必读的MySQL二进制日志指南
- 2024-10-23Binlog入门:MySQL数据库的日志管理指南