由于之前没有对group by 语句的执行顺序(执行原理)做深入的了解,所以导致在实际应用过程中出现了一些问题.举个简单的粟子,比如一个表testA中的所有数据如下图: 我现在想从testA中查询username字段,并只取前7位,取了前7位后,想让相同的数据就合并为一条,我就想当然地写了如下语句:“select substr(username,0,7) username from testA where 1=1 group by username;”心想万事大吉了,可执行之后的结果令我诧异了.…
在一次查询线上问题时发现有以下两条同样的SQL,执行后数据的顺序不一样: SELECT * FROM nns_assists_item AS asset WHERE asset.nns_assist_id='mgtv' AND asset.nns_category_id LIKE '1000003%' AND (asset.nns_tag='' OR ISNULL(asset.nns_tag) OR LOCATE(',26,',CONCAT(',',asset.nns_tag))>0) AND…
一.sql语句的执行顺序 (8)SELECT (9) DISTINCT (11) <TOP_specification> <select_list> (1) FROM <left_table> (3) <join_type> JOIN <right_table> (2) ON <join_condition> (4) WHERE <where_condition> (5) GROUP BY <group_by_lis…
1.SQL查询语句的执行顺序 (7) SELECT (8) DISTINCT <select_list> (1) FROM <left_table> (3) <join_type> JOIN <right_table> (2) ON <join_condition> (4) WHERE <where_condition> (5) GROUP BY <group_by_list> (6) HAVING <having_…