gpt4 book ai didi

php - 具体的mysql插入

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:20 25 4
gpt4 key购买 nike

我的mysql表的结构是:

CREATE TABLE IF NOT EXISTS `tb_hour_counts` (
`date` date NOT NULL,
`subid` int(20) NOT NULL,
`unique_ids` int(20) NOT NULL,
`total_ids` int(20) NOT NULL,
`unique_ips` int(20) NOT NULL,
`total_ips` int(20) NOT NULL,
`global` int(20) NOT NULL,
`time` text NOT NULL,
UNIQUE KEY `ind_1` (`date`,`time`(5),`subid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我有一组数据,例如:

$array = array(
'0' => array(
'date' => '2011-10-10',
'time' => '00:00',
'subid' => '2',
'unique_ids' => '588'
),
'1' => array(
'date' => '2011-10-10',
'time' => '00:00',
'subid' => '2',
'unique_ips' => '3'
),
'2' => array(
'date' => '2011-10-10',
'time' => '00:00',
'subid' => '2',
'total_ids' => '3995'
),
'3' => array(
'date' => '2011-10-10',
'time' => '00:00',
'subid' => '2',
'total_ips' => '1000'
),
'4' => array(
'date' => '2011-10-10',
'time' => '00:00',
'subid' => '2',
'global' => '1000'
),
'5' => array(
'date' => '2011-10-10',
'time' => '01:00',
'subid' => '3',
'unique_ids' => '766'
),
'6' => array(
'date' => '2011-10-10',
'time' => '01:00',
'subid' => '3',
'unique_ips' => '10'
),
'7' => array(
'date' => '2011-10-10',
'time' => '01:00',
'subid' => '3',
'total_ids' => '934'
),
'8' => array(
'date' => '2011-10-10',
'time' => '01:00',
'subid' => '3',
'total_ips' => '950'
),
'9' => array(
'date' => '2011-10-10',
'time' => '01:00',
'subid' => '3',
'global' => '7554'
)
);

目标是将数据插入表中。从该数组中插入数据的结果应如下所示:

result

我相信这可以通过 mysql 请求或通过为通常的插入准备数组来完成。

最佳答案

虽然很乱,但是:

// your array below
$array = array();

$newArray = array();

foreach($array as $item){
$newArray[$item['subid']]['date'] = $item['date'];
$item['time'] > $newArray[$item['subid']]['time'] ? $newArray[$item['subid']]['time'] = $item['time'] : null;
$newArray[$item['subid']]['unique_ids'] += $item['unique_ids'];
$newArray[$item['subid']]['total_ids'] += $item['total_ids'];
$newArray[$item['subid']]['unique_ips'] += $item['unique_ips'];
$newArray[$item['subid']]['total_ips'] += $item['total_ips'];
$newArray[$item['subid']]['global'] += $item['global'];
}

print_r($newArray);

这给你:

Array
(
[2] => Array
(
[date] => 2011-10-10
[time] => 00:00
[unique_ids] => 588
[total_ids] => 3995
[unique_ips] => 3
[total_ips] => 1000
[global] => 1000
)

[3] => Array
(
[date] => 2011-10-10
[time] => 01:00
[unique_ids] => 766
[total_ids] => 934
[unique_ips] => 10
[total_ips] => 950
[global] => 7554
)

)

然后你可以做类似的事情:

foreach($newArray as $key=>$value){
echo "INSERT INTO `table` ('{$value['date']}',{$key},{$value['total_ids']})"; // etc. etc.
}

关于php - 具体的mysql插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8325441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com