1

我正在跟踪许多类似网站上的访问者移动,并为每个目标分配了不同的值来跟踪目标。由于这些目标是动态创建的,我还需要动态更新它们。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);

上面的插入命令是从一个数组创建的,如下所示以及 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)呢?

4

3 回答 3

1

你有几个选择:

采用INSERT IGNORE INTO table

或者INSERT … ON DUPLICATE KEY UPDATE

查看 mysql 文档以获取有关任一命令的更多信息

于 2013-02-20T17:21:40.490 回答
1

你有所有网站的表格吗?如果是,您可以REPLACE这样使用:

REPLACE INTO goals (idsite, idgoal, name, allow_multiple, revenue)
SELECT idsite, 5, 'MMP Details Video', 1, 10 FROM sites;

这是一个包含与 table 中的 column具有相同含义的 columnsites的所有站点的表。idsiteidsitegoals

This statement inserts new record into goals table for each site defined in sites table with values idside, 5, 'MMP Details Video', 1, 10, where idsite is a value taken from idsite column of sites table. If record with the same idsite and idgoal already exists, it is replaced with new one.

于 2013-02-20T18:21:14.953 回答
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);
于 2013-02-20T18:10:46.480 回答