2011-02-01 58 views
0

我有2個表wttt將需要4個元組上升或插入,例如 表將有一個遊戲和每個球員職業生涯的條目(2)SO是4元組。關於我的頭頂,我只能想到,如果不存在,則爲player1和game執行SELECT插入else更新,爲player1和職業提供.......這看起來像很多數據庫查詢是否存在更好的方式來處理這個?在PHP MYSQL PDO現有代碼:如何最大限度地減少查詢

CREATE TABLE `standings` ( 
`id` int(11) NOT NULL AUTO_INCREMENT, 
`league_id` int(11) NOT NULL, 
`season_id` int(11) NOT NULL, 
`team_id` int(11) NOT NULL, 
`statistic_type_id` int(11) NOT NULL, 
`wlt` varchar(30) NOT NULL);  


$sth = $dbh->prepare('Select * from standings 
      where league_id=? and season_id=? and team_id=? and statistic_type_id=$? '); 

$data = $sth->execute(array($l, $s, $t, $st)); 

$data = $sth->fetchAll(); 

if ($sth->rowCount() == 0) {  
    $wlt = $w . '," . $lo . ',' . $di; 
    $sth = $dbh->prepare('INSERT INTO standings (id, league_id, season_id,  
       team_id, statistic_type_id, wlt) 
       VALUES(0, ?, ?, ?, ?, ?); 

     $data = $sth->execute(array($l, $s, $t, $st, $wlt)); 

} else { 
    foreach ($data as $row) { 
     $wlt = explode(",", $row['wlt']); 
     $wlt[0] = $wlt[0] + $w; 
     $wlt[1] = $wlt[1] + $lo;  
     $wlt[2] = $wlt[2] + $di; 
     $nwlt = implode(",", $wlt); 

     $sth = $dbh->prepare('UPDATE standings SET wlt=? 
      where league_id=? and season_id=? and team_id=? and statistic_type_id=$? '); 

     $data = $sth->execute(array($nwlt, $l, $s, $t, $st)); 
    } 
} 

回答

0
+0

嘛不知道如何。所以,如果它不退出,我會插入和替換將工作。但如果它確實存在,那麼我需要將某些字段顛倒過來。例如,如果球員贏得了比賽,那麼我需要將他的贏球場數增加1,如果他投擲了275,那麼我需要將該球場撞到275.有沒有一種方法可以替代? – Rich 2011-02-01 16:26:56