所以我有一個表格,我在其中添加了一些細節。現在,假設我提交了包含以下詳細資訊的表單:
id_s: 3
id_m: 1
quantity: 5
一切都好,資訊就存盤在表中的一行。(見下文)

接下來,如果我提交包含以下內容的表單:
id_s: 3
id_m: 1
quantity: 3
該表將包含具有相同值的兩行,而不是與資訊合并的行(見下文)

我應該放什么條件才能將它們合并為一行,這樣它就會是數量的總和?
uj5u.com熱心網友回復:
如果前兩列是唯一的,這就是我對您的描述所期望的,那么您可以這樣做:
mysql> create table mytable ( id_substatie int, id_medicament int, cantitate int );
mysql> alter table mytable add primary key (id_substatie, id_medicament);
mysql> insert into mytable values (3,1,5);
mysql> insert into mytable values (3,1,3)
on duplicate key update cantitate=cantitate values(cantitate);
mysql> select * from mytable;
-------------- --------------- -----------
| id_substatie | id_medicament | cantitate |
-------------- --------------- -----------
| 3 | 1 | 8 |
-------------- --------------- -----------
1 row in set (0.00 sec)
該運算式cantitate values(cantitate)表示該列的舊值加上您嘗試插入的新值。
uj5u.com熱心網友回復:
您基本上可以在每次插入之前為此使用觸發器
Create trigger t_sample on
sample before insert
For each row
Begin
Update sample s set
s.quantity=:OLD.quantity :NEW.quantity
where s.id_s=:NEW.id_s and s.id_m=:NEW.id_m
and s.quantity <>:NEW.quantity
;
End;
/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380031.html
下一篇:如何創建XML決議器?
