方法1:
$array = Db::connect('mysql://newxt:newxt@118.31.19.17:3306/newxt#utf8')
->table('zm_visitors_hkhk_sjd')
->where(function($query)use($market_id){
if(isset($market_id))
if($market_id == 120){
$market_id=1001;
}
$query->where('market_id',$market_id);
})
->where(function($query)use($date){
if(isset($date))
$query->where('d_date',$date);
})
->where(function($query)use($month_begin_time,$month_end_time){
if(!empty($month_begin_time) && !empty($month_end_time))
$query->whereTime('d_date','between',[$month_begin_time,$month_end_time]);
})
->where(function($query)use($year_begin_time,$year_end_time){
if(!empty($year_begin_time) && !empty($year_end_time))
$query->whereTime('d_date','between',[$year_begin_time,$year_end_time]);
})
->group('d_date')
->field([
'ifnull(sum(n_in_visitors),0) n_in_visitors',
'ifnull(sum(n_out_visitors),0) n_out_visitors',
'd_date',
'id',
])
->order(['id'=>'desc'])
->select();
方法2:
<?php
/**
* 配置文件
*/
return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '118.31.19.17',
// 数据库名
'database' => 'newxt',
// 用户名
'username' => 'newxt',
// 密码
'password' => 'newxt',
// 端口
'hostport' => '3306',
// 数据库编码默认采用utf8
'charset' => 'utf8mb4',
// 数据库表前缀
'prefix' => 'zm_',
"authcode" => 'aCGxY1fwyVVr7WTmU3',
//#COOKIE_PREFIX#
'DB_CONFIG1' => [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => 'rm-bp1z86mut35f72674wo.mysql.rds.aliyuncs.com',
// 数据库名
'database' => 'zm_pt_price_history_db',
// 用户名
'username' => 'zhumei_jysj',
// 密码
'password' => 'zhu&mei2019*JYSJ!',
// 端口
'hostport' => '3306',
// 数据库编码默认采用utf8
'charset' => 'utf8mb4',
// 数据库表前缀
'prefix' => 'zm_',
],
'DB_CONFIG2' => [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => 'rm-bp1z86mut35f72674wo.mysql.rds.aliyuncs.com',
// 数据库名
'database' => 'zm_nongmao_db',
// 用户名
'username' => 'zhumei_jysj',
// 密码
'password' => 'zhu&mei2019*JYSJ!',
// 端口
'hostport' => '3306',
// 数据库编码默认采用utf8
'charset' => 'utf8mb4',
// 数据库表前缀
'prefix' => 'nm_',
],
'DB_CONFIG3' => [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => 'rm-bp1z86mut35f72674wo.mysql.rds.aliyuncs.com',
// 数据库名
'database' => 'zm_nongmao_pxncp_db',
// 用户名
'username' => 'zhumei_jysj',
// 密码
'password' => 'zhu&mei2019*JYSJ!',
// 端口
'hostport' => '3306',
// 数据库编码默认采用utf8
'charset' => 'utf8mb4',
// 数据库表前缀
'prefix' => 'nm_',
]
];
public function createSumOld(){
$tablename ='market';
$market_id = 234;
$start_time = strtotime(date('Y-m-d H:00:00',time()))-3600;
$end_time = strtotime(date('Y-m-d H:00:00',time()))-1;
$sql = "select vhs.d_date,vhs.ip,vhs.t_start_time,vhs.market_id,vhs.t_end_time,sum(n_in_visitors) as n_in_visitors, sum(n_out_visitors) as n_out_visitors,sum(count) as count from zm_visitors_hkhk_sjd as vhs left join zm_market as m on vhs.market_id = m.id where vhs.market_id = 1002 and UNIX_TIMESTAMP(t_start_time) between $start_time and $end_time group by market_id order by sum(count) desc";
$data = Db::query($sql);
$market_info = self::getConn('DB_CONFIG2')->name($tablename)->where('id',$market_id)->field('market_name,market_code')->find();
foreach ($data as $item){
$insert['vc_market_no'] = $market_info['market_code'];
$insert['vc_market_name'] = $market_info['market_name'];
$insert['vc_place'] = $item['ip'];
$insert['d_date'] = $item['d_date'];
$insert['t_start_time'] = date('Y-m-d H:00:00',time()-3600);
$insert['t_end_time'] = date('Y-m-d H:00:00',time());
$insert['n_in_visitors'] = $item['n_in_visitors'];
$insert['n_out_visitors'] = $item['n_out_visitors'];
self::getConn('DB_CONFIG2')->name('visitors_sum')->insert($insert);
self::getConn('DB_CONFIG2')->name('visitors_flowrate_sum')->insert($insert);
self::getConn('DB_CONFIG3')->name('visitors_sum')->insert($insert);
self::getConn('DB_CONFIG3')->name('visitors_flowrate_sum')->insert($insert);
}
echo 'done';
}
private static function getConn($config){
return Db::connect(DB::getConfig($config));
}