mysql> explain select a.*,b.depname,b.memo from emp a left join dep b on a.depno = b.depno where sal>100 and a.empname like 'ab%' and a.depno=106 order by a.hiredate desc ; +----+-------------+-------+------+---------------+---------------+---------+-------+--------+-----------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+---------------+---------+-------+--------+-----------------------------+ | 1 | SIMPLE | a | ref | idx_emo_depno | idx_emo_depno | 3 | const | 974898 | Using where; Using filesort | | 1 | SIMPLE | b | ref | idx_dep_depno | idx_dep_depno | 3 | const | 1 | NULL | +----+-------------+-------+------+---------------+---------------+---------+-------+--------+-----------------------------+ 2 rows in set
mysql> select a.*,b.depname from emp a left join dep b on a.depno = b.depno where a.empname like 'ab%' and a.depno=106 and a.sal>100 order by a.hiredate desc ; +---------+---------+---------+---------+-----+---------------------+------+------+-------+------------+ | id | empno | empname | job | mgr | hiredate | sal | comn | depno | depname | +---------+---------+---------+---------+-----+---------------------+------+------+-------+------------+ | 4976754 | 4976754 | ABijwE | SALEMAN | 1 | 2021-01-23 16:46:24 | 2000 | 400 | 106 | kDpNWugzcQ | ...... +---------+---------+---------+---------+-----+---------------------+------+------+-------+------------+ 744 rows in set (0.006 sec)
select (@rowNO := @rowNo+1) AS id,bdata.* from ( select distinct a.usercode,a.username, @A1:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A1' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A1, @A2:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A2' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A2, @A3:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A3' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A3, @A4:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A4' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A4, ,(@A1+@A2+@A3+@A4) as allnum from userinfo a inner JOIN `salinvest` b on a.usercode = b.usercode where b.logdate between '2020-01-01' and '2020-12-31' order by allnum desc ) as bdata,(SELECT @rowNO:=0) b;
mysql> select (@rowNO := @rowNo+1) AS id,bdata.* from ( select (@rowNO := @rowNo+1) AS id,bdata.* from ( select distinct a.usercode,a.username, @A1:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A1' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A1, @A2:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A2' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A2, @A3:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A3' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A3, @A4:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A4' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A4, ,(@A1+@A2+@A3+@A4) as allnum from userinfo a inner JOIN `salinvest` b on a.usercode = b.usercode where b.logdate between '2020-01-01' and '2020-12-31' order by allnum desc ) as bdata,(SELECT @rowNO:=0) b; +----+------------+---------+------+------+------+------+------+--------+ | id | usercode | username | A1 | A2 | A3 | A4 |allnum +----+------------+---------+------+------+------+------+------+--------+ | 1 | 063105015 | brand | 789.00 | 1074.50 | 998.00 | 850.00 | ...... +----+------------+---------+------+------+------+------+------+--------+ 6217 rows in set (12.745 sec)
mysql> explain select (@rowNO := @rowNo+1) AS id,bdata.* from ( select distinct a.usercode,a.username, @A1:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A1' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A1, @A2:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A2' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A2, @A3:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A3' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A3, @A4:=IFNULL((select sum(c.ltimenum) from `salinvest` c where c.usercode=a.usercode AND c.gravalue='A4' and c.logdate between '2020-01-01' and '2020-12-31'),0) as A4, ,(@A1+@A2+@A3+@A4) as allnum from userinfo a inner JOIN `salinvest` b on a.usercode = b.usercode where b.logdate between '2020-01-01' and '2020-12-31' order by allnum desc ) as bdata,(SELECT @rowNO:=0) b; +----+--------------------+------------+------------+--------+------------------------+------------------------+---------+-----------------------+------+----------+-----------------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------------+------------+------------+--------+------------------------+------------------------+---------+-----------------------+------+----------+-----------------------------------------------------------+ | 1 | PRIMARY | <derived8> | NULL | system | NULL | NULL | NULL | NULL | 1 | 100 | NULL | | 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100 | NULL | | 8 | DERIVED | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | | 2 | DERIVED | b | NULL | index | idx_salinvest_complex | idx_salinvest_complex | 170 | NULL | 5 | 20 | Using where; Using index; Using temporary; Using filesort | | 7 | DEPENDENT SUBQUERY | c | NULL | ALL | idx_salinvest_complex | NULL | NULL | NULL | 5 | 20 | Using where | | 6 | DEPENDENT SUBQUERY | c | NULL | ALL | idx_salinvest_complex | NULL | NULL | NULL | 5 | 20 | Using where | | 5 | DEPENDENT SUBQUERY | c | NULL | ALL | idx_salinvest_complex | NULL | NULL | NULL | 5 | 20 | Using where | | 4 | DEPENDENT SUBQUERY | c | NULL | ALL | idx_salinvest_complex | NULL | NULL | NULL | 5 | 20 | Using where | +----+--------------------+------------+------------+--------+------------------------+------------------------+---------+-----------------------+------+----------+-----------------------------------------------------------+ 9 rows in set
第二步,上面的大结果集 t1 中的每一条记录,等同于与子查询 SQL 组成新的查询语句: select sum(c.ltimenum) from salinvest c where c.usercode in (select distinct a.usercode from userinfo a) 。