我的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'
)
);
目标是将数据插入表中。从此数组插入数据的结果应如下所示:
我相信这可以通过mysql请求,或者通过为常规插入准备数组来完成。
答案 0 :(得分:2)
这非常混乱,但是:
// 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.
}
答案 1 :(得分:1)
如果要根据日期插入,请在插入
之前使用php将数据分组到$array
答案 2 :(得分:1)
进入数据库后,订单无关紧要。
每次检索数据的调用都会自动通过默认列对数据进行排序。
如果要从数据库中提取数据,请通过SQL查询对其进行排序。 EI。
SELECT date, subid, unique_ids, total_ids, unique_ips, total_ips, global, time FROM tb_hour_counts
WHERE date = '2011-10-10'
ORDER BY time;
希望这有帮助。