我想用其他条件计算表中的行。 我在某种程度上有这个,但它不起作用。
$sql = "SELECT
(SELECT COUNT(name) FROM orders) as total,
UNION ALL
(SELECT COUNT(name) FROM orders WHERE status='1') as ok,
UNION ALL
(SELECT COUNT(name) FROM orders WHERE status='2') as ko";
答案 0 :(得分:0)
使用条件聚合:
select count(*) as total, sum(status = 1) as ok, sum(status = 2) as ko
from orders;
注意:这假设status
确实存储为数字。不要使用单引号作为数字常量(当你真的打算使用字符串时,请使用它们。)
答案 1 :(得分:0)
您可以使用count
table
多个simple query
的数据$sql = "SELECT COUNT(name) AS `total`, (SELECT COUNT(name) FROM orders WHERE status='1') as `second`, (SELECT COUNT(name) FROM orders WHERE status='2') as `third` FROM orders";
function getWeekDates($date, $start_date, $end_date) {
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("{$year}-W{$week}+1"));
if ($from < $start_date)
$from = $start_date;
$to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));
if ($to > $end_date)
$to = $end_date;
$array1 = array(
"ssdate" => $from,
"eedate" => $to,
);
return $array1;
}
$mm = date('m');
$yy = date('y');
$startdate = date($yy . "-" . $mm . "-01");
$current_date = date('Y-m-t');
$ld = cal_days_in_month(CAL_GREGORIAN, $mm, $yy);
$lastday = $yy . '-' . $mm . '-' . $ld;
$start_date = date('Y-m-d', strtotime($startdate));
$end_date = date('Y-m-d', strtotime($lastday));
$end_date1 = date('Y-m-d', strtotime($lastday . " + 7 days"));
$count_week = 0;
$week_array = array();
for ($date = $start_date; $date < $end_date1; $date = date('Y-m-d', strtotime($date . ' + 7 days'))) {
$getarray = getWeekDates($date, $start_date, $end_date);
$week_array[] = $getarray;
$count_week++;
}
echo '<pre>';
print_r($week_array);