2017-02-03 17 views
3

Я использую рамки 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 - 1, посещение appointment_icon_id2 и наоборот. Я хочу подсчет всех этих категорий. как:

appointment_icon_type_name count 
Yoga       2 
Visit       1 
Physio Therapy     1 
Diagnostic      3 

ответ

2

Все, что вам нужно это:

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 
+0

спасибо человеку свою работу охладиться вы делаете мой день человек :) – kunal