在触发器中选择返回多于1个值?

时间:2016-10-28 01:21:23

标签: mysql triggers mariadb

我在学校为足球数据库写了一个触发器:

SELECT * FROM bundesliga.tippscheine;
delimiter |
drop trigger if exists control_tip_konto|
create  trigger control_tip_konto after insert on tippscheine
for each row 
begin
declare temp INT;
set temp = (select new.TPS_Einsatz  from tipper join tippscheine where TP_ID=TPS_Tipper_ID); 
if(new.TPS_Einsatz>temp)
then
update tippscheine join tipper on TP_ID=TPS_Tipper_ID set new.TPS_Einsatz = temp where TP_ID=TPS_Tipper_ID and new.TPS_Tipper_ID=TPS_Tipper_ID;
end if;
end |
delimiter ;

它应该看一下用户的账户余额,如果他想要下注更多的钱,那么他现在有,触发器应该设置账户余额的赌注。(即某人的账户余额为20美元,想要下注22美元,触发器应该将赌注设置回20美元。

问题是select查询返回的值超过1。如果我使用“新”。在变量之前,它应该只选择触发器解析后当前添加的数据吗?

以下是用于触发器的两个表:

+--------+--------------+---------------+--------------------+------------------+-----------------+-------------+------------+---------------+---------------+
| TPS_ID | TPS_SpieleID | TPS_Tipper_ID | TPS_HeimM.         | TPS_Ausw.M.      | TPS_Getippt auf | TPS_Einsatz | TPS_Gewinn | TPS_Tore Heim | TPS_Tore Aus. |
+--------+--------------+---------------+--------------------+------------------+-----------------+-------------+------------+---------------+---------------+
|      1 |            1 |             1 | FC Bayern Muenchen | SV Werder Bremen | 1               |           5 |         10 | L             | L             |
|      2 |            1 |             2 | FC Bayern Muenchen | SV Werder Bremen | L               |           5 |         10 | 3             | 0             |
|      3 |            1 |             3 | FC Bayern Muenchen | SV Werder Bremen | 1               |           5 |         10 | L             | L             |
|      4 |            1 |             4 | FC Bayern Muenchen | SV Werder Bremen | L               |          10 |         20 | 5             | 0             |
|      5 |            2 |             1 | Borussia Dortmund  | 1. FSV Mainz 05  | 1               |           5 |         10 | L             | L             |
|      6 |            2 |             2 | Borussia Dortmund  | 1. FSV Mainz 05  | 2               |          10 |         20 | L             | L             |
|      7 |            3 |             3 | 1. FC Koeln        | SV Darmstadt 98  | X               |           5 |         10 | 2             | 2             |
|      8 |            3 |             4 | 1. FC Koeln        | SV Darmstadt 98  | L               |          10 |         20 | 1             | 2             |
|      9 |            4 |             1 | Hamburger SV       | FC Ingolstadt 04 | 1               |           5 |         10 | L             | L             |
+--------+--------------+---------------+--------------------+------------------+-----------------+-------------+------------+---------------+---------------+

还有:

+-------+-------------------+-------------------+--------------------------------+--------------------+--------------------+----------+
| TP_ID | TP_Synonym        | TP_Beigetreten am | TP_Email                       | TP_Gewonnene Tipps | TP_Verlorene Tipps | TP_Konto |
+-------+-------------------+-------------------+--------------------------------+--------------------+--------------------+----------+
|     1 | Adil_Abi          | 08.09.2016        | adil.abi@live.de               |                  0 |                  0 |        0 |
|     2 | IterranI          | 08.09.2016        | IterranI@zz.de                 |                  0 |                  0 |        0 |
|     3 | Ich1212           | 08.09.2016        | Ich1212@hiluaaa.de             |                  0 |                  0 |        0 |
|     4 | Die_Weiße_Gefahr  | 08.09.2016        | Die_Weiße_Gefahr@black.org     |                  0 |                  0 |        0 |
|     5 | G0tt              | 08.09.2016        | G0tt@outlook.com               |                  0 |                  0 |        0 |
|     6 | Grandfire         | 08.09.2016        | Grandfire@web.de               |                  0 |                  0 |        0 |
|     7 | test123testem     | 08.09.2016        | not_available@notgive.nogiven  |                  0 |                  0 |        0 |
|     8 | OliOberkrass      | 08.09.2016        | deroberkrasseoli@olvie.de      |                  0 |                  0 |        0 |
|     9 | SwaggimacSwagson  | 08.09.2016        | name@zuschwer.com              |                  0 |                  0 |        0 |
|    10 | Betonmicha        | 08.09.2016        | betonmicha@vodka.ru            |                  0 |                  0 |        0 |
|    11 | kekm8             | 08.09.2016        | cptdashsuperhengst@hotmail.biz |                  0 |                  0 |        0 |
|    12 | Duffman           | 08.09.2016        | Duffman@simpsons.com           |                  0 |                  0 |        0 |
|    13 | Bielzer           | 08.09.2016        | Bielzer@Bielzer.biezler        |                  0 |                  0 |        0 |
|    14 | ares              | 08.09.2016        | ares@greece.de                 |                  0 |                  0 |        0 |
|    15 | spidi             | 08.09.2016        | spidi@zinker.de                |                  0 |                  0 |        0 |
|    16 | AkaNixon          | 08.09.2016        | Akanixon@31er.de               |                  0 |                  0 |        0 |
|    17 | Besserwisserin    | 08.09.2016        | Besserwisserin@amk.de          |                  0 |                  0 |        0 |
+-------+-------------------+-------------------+--------------------------------+--------------------+--------------------+----------+

我希望有人可以向我解释一下,我对数据库很陌生,但还没有收集太多经验。

1 个答案:

答案 0 :(得分:0)

应该简单得多;

之类的东西
SELECT OLD.bet = LEAST(OLD.bet, account_balance) FROM ...;

UPDATE