如何使用 group by 获取每个类别的计数?
How to get the count of each category using group by?
我正在使用 cakephp 2.0 框架。我想统计每个类别的记录数。
这里我有两个表
eve_appointment_icons ->having columns- appointment_icon_id,appointment_icon_type_name
eve_appointments ->having columns- id, appointment_icon_id
我已使用此查询获取此数据:-
$this->EveAppointment->query("SELECT
a.appointment_type
d.appointment_icon_id,
a.appointment_icon_id ,
d.appointment_icon_type_name,
FROM eve_appointments a,
eve_appointment_icons d
WHERE a.appointment_type =1
AND d.appointment_icon_id = a.appointment_icon_id
UNION
SELECT
a.appointment_type
d.appointment_icon_id,
a.appointment_icon_id ,
d.appointment_icon_type_name,
FROM eve_appointments a,
eve_appointment_icons d
WHERE a.appointment_type =2
AND d.appointment_icon_id = a.appointment_icon_id
UNION
SELECT
a.appointment_type
d.appointment_icon_id,
a.appointment_icon_id ,
d.appointment_icon_type_name,
FROM eve_appointments a,
eve_appointment_icons d
WHERE a.appointment_type =3
AND d.appointment_icon_id = a.appointment_icon_id");
当我打印这个时:
Array
(
[0] => Array
(
[appointment_icon_id] => 1
[appointment_icon_type_name] => Yoga
)
[1] => Array
(
[appointment_icon_id] => 1
[appointment_icon_type_name] => Yoga
)
[2] => Array
(
[appointment_icon_id] => 2
[appointment_icon_type_name] => Visit
)
[3] => Array
(
[appointment_icon_id] => 3
[appointment_icon_type_name] => Physio Therapy
)
[4] => Array
(
[appointment_icon_id] => 4
[appointment_icon_type_name] => Diagnostic
)
[5] => Array
(
[appointment_icon_id] => 4
[appointment_icon_type_name] => Diagnostic
)
[6] => Array
(
[appointment_icon_id] => 4
[appointment_icon_type_name] => Diagnostic
)
)
现在您可以看到 appointment_icon_id
的 Yoga 是 1
,请访问 appointment_icon_id
2
,反之亦然。
我想要计算所有这些类别。
喜欢:
appointment_icon_type_name count
Yoga 2
Visit 1
Physio Therapy 1
Diagnostic 3
你只需要这个:
select d.appointment_icon_type_name, count(*)
FROM eve_appointments a,
eve_appointment_icons d
WHERE d.appointment_icon_id = a.appointment_icon_id
group by d.appointment_icon_type_name
我正在使用 cakephp 2.0 框架。我想统计每个类别的记录数。 这里我有两个表
eve_appointment_icons ->having columns- appointment_icon_id,appointment_icon_type_name
eve_appointments ->having columns- id, appointment_icon_id
我已使用此查询获取此数据:-
$this->EveAppointment->query("SELECT
a.appointment_type
d.appointment_icon_id,
a.appointment_icon_id ,
d.appointment_icon_type_name,
FROM eve_appointments a,
eve_appointment_icons d
WHERE a.appointment_type =1
AND d.appointment_icon_id = a.appointment_icon_id
UNION
SELECT
a.appointment_type
d.appointment_icon_id,
a.appointment_icon_id ,
d.appointment_icon_type_name,
FROM eve_appointments a,
eve_appointment_icons d
WHERE a.appointment_type =2
AND d.appointment_icon_id = a.appointment_icon_id
UNION
SELECT
a.appointment_type
d.appointment_icon_id,
a.appointment_icon_id ,
d.appointment_icon_type_name,
FROM eve_appointments a,
eve_appointment_icons d
WHERE a.appointment_type =3
AND d.appointment_icon_id = a.appointment_icon_id");
当我打印这个时:
Array
(
[0] => Array
(
[appointment_icon_id] => 1
[appointment_icon_type_name] => Yoga
)
[1] => Array
(
[appointment_icon_id] => 1
[appointment_icon_type_name] => Yoga
)
[2] => Array
(
[appointment_icon_id] => 2
[appointment_icon_type_name] => Visit
)
[3] => Array
(
[appointment_icon_id] => 3
[appointment_icon_type_name] => Physio Therapy
)
[4] => Array
(
[appointment_icon_id] => 4
[appointment_icon_type_name] => Diagnostic
)
[5] => Array
(
[appointment_icon_id] => 4
[appointment_icon_type_name] => Diagnostic
)
[6] => Array
(
[appointment_icon_id] => 4
[appointment_icon_type_name] => Diagnostic
)
)
现在您可以看到 appointment_icon_id
的 Yoga 是 1
,请访问 appointment_icon_id
2
,反之亦然。
我想要计算所有这些类别。
喜欢:
appointment_icon_type_name count
Yoga 2
Visit 1
Physio Therapy 1
Diagnostic 3
你只需要这个:
select d.appointment_icon_type_name, count(*)
FROM eve_appointments a,
eve_appointment_icons d
WHERE d.appointment_icon_id = a.appointment_icon_id
group by d.appointment_icon_type_name