CREATE TABLE `result` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`click_shop_maintain_id` int(11) DEFAULT NULL COMMENT '',
`corpid` bigint(20) DEFAULT NULL COMMENT '',
`platform` int(11) DEFAULT '0' COMMENT '',
`keywords` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '',
`rankingdate` date DEFAULT NULL COMMENT '',
`price` decimal(10,2) unsigned DEFAULT '0.00' COMMENT '',
`createdtime` datetime DEFAULT NULL COMMENT '',
PRIMARY KEY (`id`),
KEY `keywords` (`keywords`),
KEY `rankingdate` (`rankingdate`)
) ENGINE=InnoDB AUTO_INCREMENT=4578 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
例如建一个这样的表,以下是是PHP来处理数据,得到导出功能
倒出的数据要求是这样的:
$rows = $this->db->query("select keywords,price,rankingdate from result where click_shop_maintain_id=$click_shop_maintain_id group by keywords,price,rankingdate");
$rankingdate_result = $this->db->query("select rankingdate from result where click_shop_maintain_id=$click_shop_maintain_id group by rankingdate");
// $rows = array_chunk($rows,count($rankingdate_result),false);
$data_first = array('关键词','总消耗');
$sql = "select rankingdate,count(rankingdate) from result where click_shop_maintain_id=$click_shop_maintain_id group by rankingdate order by rankingdate desc";
$click_shop_result = $this->db->query($sql);
if ($click_shop_result) foreach ($click_shop_result as $k=> $v) {
array_push($data_first,$v['rankingdate']);
}
$result = array();
if($rows) foreach ($rows as $key => $value) {
$result[$value['keywords']][] = $value;
}
if($result) foreach ($result as $key => $value) {
foreach ($value as $k => $v) {
if($k == 0){
$keys = array_search($v['rankingdate'],$data_first)-2;
for ($i=$keys-1; $i >= 0 ; $i--) {
$data['keywords'] = $key;
$data['price'] = "-";
$data['rankingdate'] = $data_first[$i+2];
array_unshift($result[$key],$data);
}
}
$r = count($result[$key]); $f = count($data_first)-2;
if($r < $f){
$diff = $f - $r;
for ($i=0; $i < $diff ; $i++) {
$data['keywords'] = $key;
$data['price'] = "-";
$data['rankingdate'] = $data_first[$i+2+$r];
array_push($result[$key],$data);
}
}
}
}
$data_second = array('汇总');
$price2 = $this->db->getValue("select sum(price) from result where click_shop_maintain_id=$click_shop_maintain_id");
array_push($data_second,$price2);
$prices = $this->db->find("select sum(price) from result where click_shop_maintain_id=$click_shop_maintain_id group by rankingdate order by rankingdate desc");
foreach ($prices as $key => $value) {
array_push($data_second,$value['sum(price)']);
}
$datas[] = $data_first;
$datas[] = $data_second;
$corpDao = BeanFinder::get('CorporationDao');
$today = date("Y-m-d");
if ($result) foreach($result as $k=>$r){
foreach ($r as $key => $value) {
$sum[$k] += $value['price'];
}
$keywords[] = $value["keywords"];
array_push($keywords, $sum[$k]);
foreach ($r as $key => $value) {
array_push($keywords, $value['price']);
}
$datas[] = $keywords;
$keywords = array();
}
// dump($datas);