इसे आज़माएं left join
से अद्यतन मान का उपयोग करें (योग + गिनती) तालिका
update T T1
left join (
select `user`,sum(`sales`) newtotal,count(`order`) neworders
from T
group by `user`
) T2 on T1.`user` = T2.`user`
set T1.total = T2.newtotal,T1.orders = T2.neworders
डीडीएल का परीक्षण करें:
CREATE TABLE T
(`user` varchar(4), `sales` int, `order` varchar(7), `total` int, `orders` int)
;
INSERT INTO T
(`user`, `sales`, `order`, `total`, `orders`)
VALUES
('xx01', 100, 'order01', 0, 0),
('xx02', 200, 'order02', 0, 0),
('xx02', 400, 'order03', 0, 0),
('xx03', 300, 'order04', 0, 0),
('xx03', 500, 'order05', 0, 0)
;
परिणाम:
| user | sales | order | total | orders |
|------|-------|---------|-------|--------|
| xx01 | 100 | order01 | 100 | 1 |
| xx02 | 200 | order02 | 600 | 2 |
| xx02 | 400 | order03 | 600 | 2 |
| xx03 | 300 | order04 | 800 | 2 |
| xx03 | 500 | order05 | 800 | 2 |