我正在跟踪多个类似网站上的访客流量,并已分配目标以跟踪每个目标的各种值。由于这些目标是动态创建的,我还需要动态更新它们。 mysql结构的相关部分如下,它描绘了2个站点上相同的4个目标。 注意:每个站点的目标始终相同。
CREATE TABLE IF NOT EXISTS `goals` (
`idsite` int(11) NOT NULL,
`idgoal` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`allow_multiple` tinyint(4) NOT NULL,
`revenue` float NOT NULL,
PRIMARY KEY (`idsite`,`idgoal`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `goals` (`idsite`, `idgoal`, `name`, `allow_multiple`, `revenue`) VALUES
(10, 1, 'Product Benefits Video', 0, 2),
(10, 2, 'Agent Benefits Video', 0, 4),
(10, 3, 'Social Network Video', 0, 6),
(10, 4, 'MMP Overview Video', 1, 8),
(13, 1, 'Product Benefits Video', 0, 2),
(13, 2, 'Agent Benefits Video', 0, 4),
(13, 3, 'Social Network Video', 0, 6),
(13, 4, 'MMP Overview Video', 1, 8);
上面的insert命令是从数组创建的,如下所示。还有site_id。
$config['trackings'][1] = array('name'=>'Product Benefits Video', 'value'=>'2', 'multi'=>'0');
$config['trackings'][2] = array('name'=>'Agent Benefits Video', 'value'=>'3', 'multi'=>'0');
$config['trackings'][3] = array('name'=>'Social Network Video', 'value'=>'4', 'multi'=>'0');
$config['trackings'][4] = array('name'=>'MMP Overview Video', 'value'=>'5', 'multi'=>'1');
我永远不需要删除目标,但我想知道如何更改值,如果目标不存在,请将其添加到所有网站。
如果我按如下方式提供新数组的示例:
$config['trackings'][1] = array('name'=>'Product Benefits Video', 'value'=>'6', 'multi'=>'0');
$config['trackings'][2] = array('name'=>'Agent Benefits Video', 'value'=>'7', 'multi'=>'0');
$config['trackings'][3] = array('name'=>'Social Network Video', 'value'=>'8', 'multi'=>'1');
$config['trackings'][4] = array('name'=>'MMP Overview Video', 'value'=>'9', 'multi'=>'1');
$config['trackings'][5] = array('name'=>'MMP Details Video', 'value'=>'10', 'multi'=>'1');
我想添加目标5并更新每个网站的现有目标的值和allow_multiple。
我考虑过截断表并从头开始重写所有值但是有数千个站点。 我也玩过替换,插入更新并最终拉出我的头发。
有没有更简单的方法来清空该地段呢?
编辑: 最终结果将如下所示
(10, 1, 'Product Benefits Video', 0, 6),
(10, 2, 'Agent Benefits Video', 0, 7),
(10, 3, 'Social Network Video', 1, 8),
(10, 4, 'MMP Overview Video', 1, 9),
(10, 5, 'MMP Details Video', 1, 10),
(13, 1, 'Product Benefits Video', 0, 6),
(13, 2, 'Agent Benefits Video', 0, 7),
(13, 3, 'Social Network Video', 1, 8),
(13, 4, 'MMP Overview Video', 1, 9);
(13, 5, 'MMP Details Video', 1, 10);
编辑:这将很容易地完成所有更新
foreach($config['trackings'] as $id => $track)
{
$s = "UPDATE `goals` SET `revenue` = ".$track['value'].", `allow_multiple` = ".$track['multi']." WHERE `idgoal` = '".$id."'";
}
但是如何同时添加缺失的目标(5)?
答案 0 :(得分:1)
您有几个选择:
使用INSERT IGNORE INTO table
或INSERT … ON DUPLICATE KEY UPDATE
查看mysql文档以获取有关任一命令的更多信息
答案 1 :(得分:1)
您是否拥有所有网站的表格?如果是,您可以像这样使用REPLACE
:
REPLACE INTO goals (idsite, idgoal, name, allow_multiple, revenue)
SELECT idsite, 5, 'MMP Details Video', 1, 10 FROM sites;
此处sites
是包含列idsite
的所有网站的表格,其含义与表格idsite
中的列goals
相同。
此语句将goals
表格中的新记录插入sites
表中定义的值为idside, 5, 'MMP Details Video', 1, 10
的每个网站,其中idsite
是取自idsite
的值sites
表的列。如果已存在相同idsite
和idgoal
的记录,则会将其替换为新记录。
答案 2 :(得分:0)
$s = "SELECT MAX(`idgoal`) AS slice FROM goals"; //4
$slice = 4;
$track_update = array_slice($config['trackings'], 0, $slice, true);
foreach($track_update as $id => $track){ //4 queries
$s = "UPDATE `goals` SET `revenue` = ".$track['value'].",
`allow_multiple` = ".$track['multi']." WHERE `idgoal` = '".$id."'";
}
//now to add new values
$s = "SELECT DISTINCT(`idsite`) AS mid FROM goals"; //array(10,13)
$result = array(10,13);
$track_add = array_slice($config['trackings'], $slice, count($config['trackings']), true);
foreach($track_add as $id => $track){ //1 query for each site
foreach($result as $idsite){
$sx[] = "(
".$idsite.", ".$id.",
'".$track['name']."',
".$track['multi'].",
".$track['value']."
)";
}
$s = "INSERT INTO `goals`
(`idsite`, `idgoal`, `name`, `allow_multiple`, `revenue`)
VALUES ".implode(',',$sx);
}
总共5个查询:
UPDATE `goals` SET `revenue` = 6, `allow_multiple` = 0 WHERE `idgoal` = '1'
UPDATE `goals` SET `revenue` = 7, `allow_multiple` = 0 WHERE `idgoal` = '2'
UPDATE `goals` SET `revenue` = 8, `allow_multiple` = 1 WHERE `idgoal` = '3'
UPDATE `goals` SET `revenue` = 9, `allow_multiple` = 1 WHERE `idgoal` = '4'
INSERT INTO `goals` (`idsite`, `idgoal`, `name`, `allow_multiple`, `revenue`)
VALUES
(10, 5, 'MMP Details Video', 1, 10),
(13, 5, 'MMP Details Video', 1, 10);