先展示演示用表:
这是可以执行的,因为主查询字段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,';',',') 进行转换;
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) > 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 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
欢迎评论指正~