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));
}
}
嘛不知道如何。所以,如果它不退出,我会插入和替换将工作。但如果它确实存在,那么我需要将某些字段颠倒过来。例如,如果球员赢得了比赛,那么我需要将他的赢球场数增加1,如果他投掷了275,那么我需要将该球场撞到275.有没有一种方法可以替代? – Rich 2011-02-01 16:26:56