分表规则:按月分表,每个月一张表,表的字段和类型都相同。 如 test_table_202011
查询方法:
$start_date = strtotime('2020-01-01 00:00:00'); //开始时间戳
$end_date = strtotime('2020-07-01 23:59:59'); //截至时间戳
$month_begin = date('Ym', $start_date); //开始年月
$month_end = date('Ym', $end_date); //截至年月
$month_plus = 1;
$month_next = date('Ym', strtotime("+{$month_plus} months", $start_date));
$UNION_SQL = "SELECT * FROM test_table_{$month_begin}";
while(intval($month_next) <= intval($month_end)){
$UNION_SQL .= " UNION ALL SELECT * FROM test_table_{$month_next}";
$month_plus += 1;
$month_next = date('Ym', strtotime("+{$month_plus} months", $start_date));
}
$sql = "SELECT * FROM ($UNION_SQL) t WHERE 1 AND create_time BETWEEN $start_date AND $end_date";
$dt = DB::query($sql);
可根据自己的需求封装成函数调用。