table1
id name je1 je2 je3
1 aaa
2 bbb
2 ccc
table2
id xm_dm je
1 01 100
1 01 110
1 02 50
1 03 200
2 02 330
2 02 20
2 03 80
能否用一句UPDATE 實作 table1表中je1,je2,je3的資料輸入.當table1.id=table2.id且table2.xm_dm=01時,table1.je1等于table2.je累計,當table1.id=table2.id且table2.xm_dm=02時,table1.je2等于table2.je累計,當table1.id=table2.id且table2.xm_dm=03時,table1.je3等于table2.je累計.結果為
table1
id name je1 je2 je3
1 aaa 210 50 200
2 bbb 350 80
2 ccc
謝謝!
uj5u.com熱心網友回復:
好象不行。只能用觸發器,uj5u.com熱心網友回復:
create table tb1(id int , name varchar(10) , je1 int , je2 int , je3 int)
insert into tb1 values(1, 'aaa',0,0,0)
insert into tb1 values(2, 'bbb',0,0,0)
insert into tb1 values(2, 'ccc',0,0,0)
create table tb2(id int , xm_dm varchar(10) , je int)
insert into tb2 values(1, '01', 100 )
insert into tb2 values(1, '01', 110 )
insert into tb2 values(1, '02', 50 )
insert into tb2 values(1, '03', 200 )
insert into tb2 values(2, '02', 330 )
insert into tb2 values(2, '02', 20 )
insert into tb2 values(2, '03', 80 )
go
update tb1
set je1 = isnull((select sum(je) from tb2 where id = tb1.id and tb2.xm_dm = '01'),0),
je2 = isnull((select sum(je) from tb2 where id = tb1.id and tb2.xm_dm = '02'),0),
je3 = isnull((select sum(je) from tb2 where id = tb1.id and tb2.xm_dm = '03'),0)
select * from tb1
drop table tb1 , tb2
/*
id name je1 je2 je3
----------- ---------- ----------- ----------- -----------
1 aaa 210 50 200
2 bbb 0 350 80
2 ccc 0 350 80
(所影響的行數為 3 行)
*/
不知道你的
2 ccc
這條記錄為何je1,je2,je3沒有值?
uj5u.com熱心網友回復:
不合理uj5u.com熱心網友回復:
感謝專家解答,糾正table1中第三行id應該為3.
table1
id name je1 je2 je3
1 aaa
2 bbb
3 ccc
uj5u.com熱心網友回復:
感謝專家解答,糾正table1中第三行id應該為3.table1
id name je1 je2 je3
1 aaa
2 bbb
3 ccc
uj5u.com熱心網友回復:
感謝專家解答,糾正table1中第三行id應該為3.table1
id name je1 je2 je3
1 aaa
2 bbb
3 ccc
uj5u.com熱心網友回復:
一條陳述句不行,UPDATE不支持轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/107704.html
標籤:數據庫相關
上一篇:請教高手
下一篇:PB和SQL中的日期型別?
