我在理解重复键更新sql语法时遇到了问题。他们都使用非常小的例子。
我需要去:
INSERT INTO skaters (id,name,team,position,games_played,goals,assists,points,plus_minus,penalty_minutes,power_play_goals,power_play_points,shorthand_goals,shorthand_points,winning_goals,overtime_goals,shots,shot_percentage,time_on_ice_pg,faceoff_win_percentage) VALUES (:id,:name,:team,:position,:games_played,:goals,:assists,:points,:plus_minus,:penalty_minutes,:power_play_goals,:power_play_points,:shorthand_goals,:shorthand_points,:winning_goals,:overtime_goals,:shots,:shot_percentage,:time_on_ice_pg,:faceoff_win_percentage)
ON DUPLICATE KEY UPDATE name=values(name),team=values(team),position=values(position),games_played=values(games_played),goals=values(goals),assists=values(assists),points=values(points),plus_minus=values(plus_minus),penalty_minutes=values(penalty_minutes),power_play_goals=values(power_play_goals),power_play_points=values(power_play_points),shorthand_goals=values(shorthand_goals),shorthand_points=values(shorthand_points),winning_goals=values(winning_goals),overtime_goals=values(overtime_goals),shots=values(shots),shot_percentage=values(shot_percentage),time_on_ice_pg=values(time_on_ice_pg),faceoff_win_percentage=values(faceoff_win_percentage);我想知道是否有快捷方式来更新前面提到的所有值。或者仅仅是一种更简洁的方式或写作,而不是一大堆标题(title=values)。例如,我可以使用(name,team,position,...)=values(name,team,position,...)吗?
发布于 2015-03-14 01:43:35
据我所知,您必须分别指定每一个。
通过从数组构建它,可以省去一些麻烦/打字错误:
$flds = array('name', 'team', 'position', ...)
$sql = 'INSERT INTO table (id';
foreach ($flds as $f)
$sql .= ','.$f;
$sql .= ') VALUES (:id';
foreach ($flds as $f)
$sql .= ',:'.$f;
$sql .= ') ON DUPLICATE KEY UPDATE ';
$sep = '';.
foreach ($flds as $f) {
$sql .= "$sep$f=values($f)";
$sep = ', ';
}https://stackoverflow.com/questions/29038428
复制相似问题