WITH AS 與增刪改查結合用法

2021-10-11 19:01:50 字數 3664 閱讀 4569

注意:1. with必須緊跟引用的select語句

2.with建立的臨時表必須被引用,否則報錯

4.1與select查詢語句結合使用

查詢同乙個單據編號對應的借款單和核銷單中,借款金額不相等的單據

with verificationinfo as

(select ment.fnumber,

sum(t.famount) vloansum,

ment.fnumber "單據編號",

sum(t.famount) "核銷單中借款總額"

from shenzhenjm.t_finance_expenseremburseitem t

left join shenzhenjm.t_finance_expenserembursement ment

on ment.fid = t.fkrembursementid

where 1 = 1

group by ment.fnumber),

loaninfo as

(select ment.fnumber,

sum(t.famount) loansum,

ment.fnumber "單據編號",

sum(t.famount) "借款單中借款總額"

from shenzhenjm.t_finance_expenseremburseitem2 t

left join shenzhenjm.t_finance_expenserembursement ment

on ment.fid = t.fkrembursementid

where 1 = 1

group by ment.fnumber)

select *

from verificationinfo v, loaninfo l

where l.fnumber = v.fnumber

and l.loansum != v.vloansum;

4.2與insert結合使用

如下的with as語句,不能放在insert前,而是放在緊接著要呼叫的地方前

要求將同乙個單據編號對應的借款單和核銷單中,借款金額不相等的單據,對應的借款單刪除,並將對應的核銷單插入到借款單表中 (借款單和核銷單表結構完全一樣)

insert into t_finance_expenseremburseitem2

(fid,

fkrembursementid,

famount,

fkcreatebyid,

fcreatetime,

fkcuid,

fkcosttypeid,

fcosttypename)

with verificationinfo as

(select ment.fnumber,

sum(t.famount) vloansum,

ment.fnumber "單據編號",

sum(t.famount) "核銷單中借款總額"

from shenzhenjm.t_finance_expenseremburseitem t

left join shenzhenjm.t_finance_expenserembursement ment

on ment.fid = t.fkrembursementid

where 1 = 1

group by ment.fnumber),

loaninfo as

(select ment.fnumber,

sum(t.famount) loansum,

ment.fnumber "單據編號",

sum(t.famount) "借款單中借款總額"

from shenzhenjm.t_finance_expenseremburseitem2 t

left join shenzhenjm.t_finance_expenserembursement ment

on ment.fid = t.fkrembursementid

where 1 = 1

group by ment.fnumber)

select sys_guid(),

ment.fid,

t.famount,

ment.fkcreatebyid,

ment.fcreatetime,

ment.fkcuid,

t.fkcosttypeid,

t.fcosttypename

from t_finance_expenseremburseitem t

left join t_finance_expenserembursement ment

on ment.fid = t.fkrembursementid

where 1 = 1

and exists (select *

from verificationinfo v, loaninfo l

where l.fnumber = v.fnumber

and l.loansum != v.vloansum

and v.fnumber = ment.fnumber);

4.3 與delete刪除結合使用

delete from t_finance_expenseremburseitem2 item2

where exists(with temp as (select t.fnumber,

sum(item1.famount) vloansum,

sum(item1.frealityamount) vsum,

sum(item2.famount) loansum

from t_finance_expenserembursement t

left join t_finance_expenseremburseitem item1

on item1.fkrembursementid = t.fid

left join t_finance_expenseremburseitem2 item2

on item2.fkrembursementid = t.fid

where 1 = 1

and t.frembursementtype = 'loan_report'

and to_char(t.fcreatetime, 'yyyy') > '2017'

group by t.fnumber

order by t.fnumber asc)

select 1

from temp t

left join t_finance_expenserembursement ment

on t.fnumber = ment.fnumber

left join t_finance_expenseremburseitem2 item

on item.fkrembursementid = ment.fid

where t.vloansum != t.loansum

and item.fid = item2.fid);

4.4與update結合使用

update dest b

set b.name =

(with t as (select * from temp)

select a.name from temp a where a.id = b.id)

laravel Eloquent 增刪改查結果

增加如下內容 function report if exception instanceof illuminate database queryexception function render if exception instanceof illuminate database queryexc...

Python 列表 字典的增刪改查結合案例

字 典 增 新的關鍵 值 刪除 刪除 鍵 改 已存在的key new值 查 get key 列表 1 2,3.14 字典 下面是例子 coding utf 8 資訊列表 infor 功能提示 print 50 print 資訊管理系統 print 1.新增乙個新的資訊 print 2.刪除乙個人的資...

Vue結合後台的增刪改案例

首先列表內容還是與之前的列表內容類似,不過此處我們會採用vue中資料請求的方式來實現資料的增刪。那麼我們使用的vue第三方元件就是vue resource,vue發起請求的方式與jquery的ajax相似,組要是請求位址與引數。和方法 首先我們先看到的是列表請求 id name ctime oper...