2014-01-14 183 views
1
mysql> select * from Teams; 
+-----------+---------+ 
| team_name | team_id | 
+-----------+---------+ 
| India  |  1 | 
| England |  2 | 
| Germany |  3 | 
| Italy  |  4 | 
| Spain  |  5 | 
| Belgium |  6 | 
| Brazil |  7 | 
| France |  8 | 
+-----------+---------+ 
8 rows in set (0.00 sec) 


select * from Players where team_id=7 or team_id=8; 
+-----------+----------------+------------+------+--------+--------+---------+ 
| player_id | player_name | position | age | height | weight | team_id | 
+-----------+----------------+------------+------+--------+--------+---------+ 
|  130 | Jefferson  | Goalkeeper | 25 |  6 |  90 |  7 | 
|  131 | Dani Alves  | Defender | 30 |  6 |  65 |  7 | 
|  132 | David Luiz  | Defender | 26 |  6 |  73 |  7 | 
|  133 | Thiago Silva | Defender | 29 |  6 |  69 |  7 | 
|  134 | Marcelo  | Defender | 24 |  5 |  65 |  7 | 
|  135 | Paulinho  | Midfielder | 27 |  6 |  68 |  7 | 
|  136 | Ramires  | Midfielder | 25 |  6 |  59 |  7 | 
|  137 | Oscar   | Midfielder | 21 |  5 |  61 |  7 | 
|  138 | Lucas Moura | Striker | 19 |  5 |  62 |  7 | 
|  139 | Neymar   | Striker | 21 |  5 |  63 |  7 | 
|  140 | Alex Pato  | Striker | 25 |  6 |  69 |  7 | 
|  150 | Hugo Lloris | Goalkeeper | 28 |  6 |  75 |  8 | 
|  151 | Mathiu Debuchy | Defender | 25 |  6 |  70 |  8 | 
|  152 | Philip Mexes | Defender | 34 |  6 |  73 |  8 | 
|  153 | Younes Kaboul | Defender | 29 |  6 |  81 |  8 | 
|  154 | Patrice Evra | Defender | 34 |  6 |  75 |  8 | 
|  155 | Paul Pogba  | Midfielder | 21 |  6 |  68 |  8 | 
|  156 | Samir Nasri | Midfielder | 27 |  6 |  69 |  8 | 
|  157 | Yohan Cabaye | Midfielder | 26 |  6 |  64 |  8 | 
|  158 | Frank Ribery | Striker | 28 |  5 |  63 |  8 | 
|  159 | Oliver Giroud | Striker | 25 |  6 |  74 |  8 | 
|  160 | Karim Benzema | Striker | 24 |  6 |  71 |  8 | 
+-----------+----------------+------------+------+--------+--------+---------+ 
22 rows in set (0.00 sec) 



mysql> select * from Matches where match_id=4; 
+----------+------------+----------+----------+-----------+-----------+ 
| match_id | match_date | hometeam | awayteam | homescore | awayscore | 
+----------+------------+----------+----------+-----------+-----------+ 
|  4 | 2014-06-28 |  7 |  8 |   0 |   0 | 
+----------+------------+----------+----------+-----------+-----------+ 
1 row in set (0.00 sec) 

如何更新表(hometeam,awayteam外鍵Teams.team_id)如果在另一個表值插入

mysql> select * from livescore; 
+----------+-----------+----------+---------+ 
| score_no | player_id | match_id | team_id | 
+----------+-----------+----------+---------+ 
|  1 |  155 |  4 |  8 | 
+----------+-----------+----------+---------+ 
1 row in set (0.00 sec) 

(player_id,match_id,team_id are foreign keys of Players,Matches,Teams) 我有一個Players臺,其中我有所有玩家的信息。

我想要的是當一個新行插入'livescore'homescore,awayscore應該相應地增加。即如果玩家屬於team_id=8livescore那麼awayscore其中awayteam=8必須增加+1

+1

可能適用於http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html :) – Pavel

回答

0

有與一個小竅門,你可以做一個單一的查詢使用ON DUPLICATE KEY結構:

INSERT INTO `table` (`field_a`, `field_b`, `field_c`) 
VALUES('val_a', 'val_b', 'val_c') 
ON DUPLICATE KEY UPDATE `field_c` = `field_c` + 1; 

的重複鍵會自動用作使用「ON DUPLICATE KEY」時,WHERE,所以這是非常舒服。

請記住,您嘗試插入的rowa或至少一個密鑰必須是拒絕重複條目的主鍵或唯一索引,因此此構造將起作用。

+0

是否可以更新現有的行而不是使用上面的代碼插入新行? Steini – hotspur27

+0

爲此,您應該考慮創建一個觸發器,Pavel在您的評論中說。使用觸發器,您可以爲表提供預定義的行爲,例如字段得到更新時,您可以執行自定義代碼,例如插入或更新到另一個表中。但是,你確定你想要兩張分數表嗎?你不能一次在livescore表中創建一個每個分數類型的列嗎? – Steini