我有多个表(相同的列结构),我希望在两个日期范围之间有计数行。现在它给了我每张桌子的所有记录。每个表有大约700K记录,这使得搜索速度也很慢。 我没有得到正确的查询。 以下查询是我到目前为止的。
$monday = '2018-01-15';
$tuesday = '2018-01-16';
SELECT count(datetime) FROM uptime_m1 WHERE datetime
BETWEEN '$monday' and'$tuesday' and status = 'idle'
UNION ALL
SELECT (datetime) FROM uptime_m2 WHERE datetime
BETWEEN '$monday' and'$tuesday' and status = 'idle';
PHP代码:
if ($result = $mysqli->query("
SELECT count(datetime) FROM uptime_m1 WHERE datetime
BETWEEN '$monday' and'$tuesday' and status = 'idle'
UNION ALL
SELECT (datetime) FROM uptime_m2 WHERE datetime
BETWEEN '$monday' and'$tuesday' and status = 'idle';
")) {
$row_cnt = $result->num_rows;
$count_m1_idle_monday = $row_cnt;
$result->close();
}
答案 0 :(得分:0)
您可以使用子请求来计算每个表并对结果求和。
select
(select count(*) from uptime_m1
where datetime between '$monday' and '$tuesday' and status = 'idle')
+ (select count(*) from uptime_m2
where datetime between '$monday' and '$tuesday' and status = 'idle')
as nbrows;