2011-11-30 3 views
0

내 MySQL의 테이블의 구조는 다음 목표는 테이블에 데이터를 삽입하는 것입니다특정 MySQL의 삽입

$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' 
    ) 
); 

:

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; 

내가 좋아하는 데이터의 배열을 가지고있다. 이 배열에서 데이터를 삽입의 결과는 다음과 같아야합니다

result

난이 MySQL의 요청에 의해, 또는 일반적인 삽입의 배열을 준비하여 중 하나를 수행 할 수 있으리라 생각합니다.

+0

무엇이 문제입니까? 문제가 무엇입니까? – KingCrunch

답변

2

를 삽입하기 전에 날짜의 따라 삽입 할 경우 매우 지저분하지만 :

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 
     ) 

) 

:

// 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); 

이 당신에게 제공 그러면 다음과 같이 할 수 있습니다.

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

서브 어레이에 'subid' 필드가 필요하다는 것을 제외하고 이것은 유일한 정답 인 것 같습니다. – mintobit

+0

@hookman 아니, 그 배열 키 ('$ key') - 중복 데이터가 필요하지 않습니다. – Prisoner

+0

@hookman : 꼭해야만한다면'! isset ($ newArray [$ subid '] ['subid '])를 추가하면됩니다. $ newArray [$ item [ 'subid']] [ 'subid'] = $ item [ 'subid'] : null;'처음 foreach에. – Prisoner

1

당신이 그룹 PHP를 사용 $array의 데이터

+0

"날짜에 따라 삽입"이란 무엇을 의미합니까? 데이터베이스는 그것에 대해 신경 쓰지 않습니다. – KingCrunch

+0

'$ array' 요소 중 일부는 'unique_ids'와 같은 데이터를 포함하지 않지만 다른 항목은'total_ids '를 포함합니다. 스크린 샷에는 '날짜 : 시간'별로 그룹화 된 모든 데이터가 있습니다. –

+0

'INSERT INTO 테이블 (날짜, 시간, subid, unqiue_ids, unique_ips, total_ids, total_ips, 전역) VALUES (...)'이것은 하나의 행입니다. 그러나 배열에서 각 하위 배열에는 날짜, 시간, subid 및 unique_ids 또는 unqiue_ips 또는 total_ips 또는 total_ids 또는 global이 포함됩니다. – mintobit

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; 

희망이 도움이됩니다.

+0

이 답변은 내 질문과 어떻게 연결되어 있습니까? – mintobit

+0

"데이터를 검색하는 모든 호출은 자동으로 기본 열을 통해 데이터를 통해 정렬됩니다." 아니요, 'ORDER BY'를 통해 지정하지 않는 한 특정 주문을 수락 할 수 없습니다 (다음 문장을 직접 말하십시오)) – KingCrunch