首頁 > 軟體

MySQL with語句講解

2022-11-13 14:01:17

備註:測試資料庫版本為MySQL 8.0

今天我們來聊聊MySQL的with語句
對於邏輯複雜的sql,with可以大大減少臨時表的數量,提升程式碼的可讀性、可維護性

MySQL 8.0終於開始支援with語句了,對於複雜查詢,可以不用寫那麼多的臨時表了。

如需要scott使用者下建表及錄入資料語句,可參考:
scott建表及錄入資料sql指令碼

語句結構:

with subquery_name1 as (subquery_body1),
        subquery_name2 as (subquery_body2)
...
select * from subquery_name1 a, subquery_name2 b
where a.col = b.col
...

優勢

  • – 程式碼模組化
  • – 程式碼可讀性增強
  • – 相同查詢唯一化

一.提升程式碼的可讀性和可維護性

需求:求每個部門的平均工資,以及剔除薪資低於1000的實習人員之後的平均工資

-- 求每個部門的平均工資,以及剔除薪資低於1000的實習人員之後的平均工資
-- 主查詢的from後面跟了2個臨時表,程式可讀性不佳
select d.deptno, tmp1.avg_sal avg_sal1, tmp2.avg_sal avg_sal2
  from dept d
  left join (select e1.deptno, round(avg(ifnull(e1.sal, 0)), 2) avg_sal
               from emp e1
              group by e1.deptno) tmp1
    on d.deptno = tmp1.deptno
  left join (select e1.deptno, round(avg(ifnull(e1.sal, 0)), 2) avg_sal
               from emp e1
              where e1.sal > 1000
              group by e1.deptno) tmp2
    on d.deptno = tmp2.deptno;
    
    
-- 求每個部門的平均工資,以及剔除薪資低於1000的實習人員之後的平均工資
-- 2個臨時表的定時語句通過with封裝成子查詢了,程式可讀性增強
with tmp1 as
 (select e1.deptno, round(avg(ifnull(e1.sal, 0)), 2) avg_sal
    from emp e1
   group by e1.deptno),
tmp2 as
 (select e1.deptno, round(avg(ifnull(e1.sal, 0)), 2) avg_sal
    from emp e1
   where e1.sal > 1000
   group by e1.deptno)
select d.deptno, tmp1.avg_sal avg_sal1, tmp2.avg_sal avg_sal2
  from dept d
  left join tmp1
    on d.deptno = tmp1.deptno
  left join tmp2
    on d.deptno = tmp2.deptno;
mysql> -- 求每個部門的平均工資,以及剔除薪資低於1000的實習人員之後的平均工資
mysql> -- 主查詢的from後面跟了2個臨時表,程式可讀性不佳
mysql> select d.deptno, tmp1.avg_sal avg_sal1, tmp2.avg_sal avg_sal2
    ->   from dept d
    ->   left join (select e1.deptno, round(avg(ifnull(e1.sal, 0)), 2) avg_sal
    ->                from emp e1
    ->               group by e1.deptno) tmp1
    ->     on d.deptno = tmp1.deptno
    ->   left join (select e1.deptno, round(avg(ifnull(e1.sal, 0)), 2) avg_sal
    ->                from emp e1
    ->               where e1.sal > 1000
    ->               group by e1.deptno) tmp2
    ->     on d.deptno = tmp2.deptno;
+--------+----------+----------+
| deptno | avg_sal1 | avg_sal2 |
+--------+----------+----------+
|     10 |  2916.67 |  2916.67 |
|     20 |  2175.00 |  2518.75 |
|     30 |  1566.67 |  1690.00 |
|     40 |     NULL |     NULL |
+--------+----------+----------+
4 rows in set (0.00 sec)

mysql>
mysql>
mysql> -- 求每個部門的平均工資,以及剔除薪資低於1000的實習人員之後的平均工資
mysql> -- 2個臨時表的定時語句通過with封裝成子查詢了,程式可讀性增強
mysql> with tmp1 as
    ->  (select e1.deptno, round(avg(ifnull(e1.sal, 0)), 2) avg_sal
    ->     from emp e1
    ->    group by e1.deptno),
    -> tmp2 as
    ->  (select e1.deptno, round(avg(ifnull(e1.sal, 0)), 2) avg_sal
    ->     from emp e1
    ->    where e1.sal > 1000
    ->    group by e1.deptno)
    -> select d.deptno, tmp1.avg_sal avg_sal1, tmp2.avg_sal avg_sal2
    ->   from dept d
    ->   left join tmp1
    ->     on d.deptno = tmp1.deptno
    ->   left join tmp2
    ->     on d.deptno = tmp2.deptno;
+--------+----------+----------+
| deptno | avg_sal1 | avg_sal2 |
+--------+----------+----------+
|     10 |  2916.67 |  2916.67 |
|     20 |  2175.00 |  2518.75 |
|     30 |  1566.67 |  1690.00 |
|     40 |     NULL |     NULL |
+--------+----------+----------+
4 rows in set (0.00 sec)

mysql>

二.with遞迴

用with遞迴構造數列

-- 用with遞迴構造1-10的資料
with RECURSIVE c(n) as
 (select 1   union all select n + 1 from c where n < 10)
select n from c;
-- 用with遞迴構造1-10的資料
mysql> with RECURSIVE c(n) as
    ->  (select 1   union all select n + 1 from c where n < 10)
    -> select n from c;
+------+
| n    |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
|    7 |
|    8 |
|    9 |
|   10 |
+------+
10 rows in set (0.00 sec)

用with遞迴構造級聯關係

with RECURSIVE emp2(ename,empno,mgr,lvl)
  as
   (select ename, empno, mgr, 1 lvl from emp where mgr is null
    union all
    select emp.ename, emp.empno, emp.mgr, e2.lvl+1
      from emp, emp2 e2
     where emp.mgr = e2.empno
   )
select lvl,
      concat(repeat('**',lvl),ename) nm
  from emp2
 order by lvl,ename
;
mysql> with RECURSIVE emp2(ename,empno,mgr,lvl)
    ->   as
    ->    (select ename, empno, mgr, 1 lvl from emp where mgr is null
    ->     union all
    ->     select emp.ename, emp.empno, emp.mgr, e2.lvl+1
    ->       from emp, emp2 e2
    ->      where emp.mgr = e2.empno
    ->    )
    -> select lvl,
    ->       concat(repeat('**',lvl),ename) nm
    ->   from emp2
    ->  order by lvl,ename
    -> ;
+------+---------------+
| lvl  | nm            |
+------+---------------+
|    1 | **KING        |
|    2 | ****BLAKE     |
|    2 | ****CLARK     |
|    2 | ****JONES     |
|    3 | ******ALLEN   |
|    3 | ******FORD    |
|    3 | ******JAMES   |
|    3 | ******MARTIN  |
|    3 | ******MILLER  |
|    3 | ******SCOTT   |
|    3 | ******TURNER  |
|    3 | ******WARD    |
|    4 | ********ADAMS |
|    4 | ********SMITH |
+------+---------------+
14 rows in set (0.00 sec)

到此這篇關於MySQL with語句小結的文章就介紹到這了,更多相關mysql with語句內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com