exists的用途: exists 用于查看子查詢是否有符合條件的資料,回傳值是boolean,一般用于sql陳述句的排查(符合條件的、不符合條件的)
1. 首先我們設定一個業務場景: 以工單系統為例, 分為業務系統入云申請、系統上線、資源變更、系統退出四大功能
2. 設計資料庫:
1. 工單的基本資訊部分是不變的,所以將工單基礎部分設為工單基礎資料表(order),工單的每一次操作(入云、上線、變更、退出)都會復制一條工單基礎資訊并設定工單型別(order_type)
2. 工單入云表、記錄工單入云資訊(order_cloud)
3. 工單上線表、記錄工單上線資訊(order_online)
4. 工單變更表、記錄工單變更資訊(order_alteration)
5. 工單退出表、記錄工單退出資訊(order_secede)
我們將工單分為五個表, 一個基礎表和四個業務表,業務主鍵為基礎資料(order)的主鍵
3. exists 的使用
引數設定
sign_type: 審批型別
order_type:工單型別(0入云、1上線、2變更、3退出)
案例1: 查詢所有可上線的工單 not exists的使用
條件1: 已入云成功
條件2:工單沒有退出與審核中的
條件3:工單沒有上線的
// 查詢所有已入云成功的工單
select * from order o where o.status=1 and o.sign_type=已通過入云 and o.order_type=0
// 工單上線審批中或審批通過的不允許再上線
and not exists(select * from order_online on where o.id=on.id and on.order_type=1 and on.sign_type in (審批通過、審批中) )
// 工單退出審批中或審批通過的不允許再上線
and not exists(select * from order_secede sec where o.id=sec.id and sec.order_type=3 and sec.sign_type in (審批通過、審批中) )
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/240879.html
標籤:其他
上一篇:Mysqldump邏輯備份恢復與binglog日志恢復
下一篇:bash: scripts/mysql_install_db: /usr/bin/perl: 壞的解釋器: 沒有那個檔案或目錄
