user1857116 user1857116 - 4 months ago 7
MySQL Question

Multiple rows in a MySQL join

I have this query / PHP code:

public function get_days_appointments($day) {
$day_start = date('Y-m-d 00:00:00', $day);
$day_end = date('Y-m-d 23:59:59', $day);
return $this->db->select('e.id,
e.start_datetime,
e.end_datetime,
e.hash,
e.id_google_calendar,
u1.first_name AS customer_first_name,
u1.last_name AS customer_last_name,
u1.email AS customer_email,
u1.phone_number AS customer_phone_number,
u2.first_name AS provider_first_name,
u2.last_name AS provider_last_name,
u2.email AS provider_email,
u2.phone_number AS provider_phone_number,
s.name,
s.duration,
s.price,
s.description,
t.push_identifier')
->from('ea_appointments e')
->join('ea_users u1', 'e.id_users_customer = u1.id','left')
->join('ea_users u2', 'e.id_users_provider = u2.id','left')
->join('ea_services s', 'e.id_services = s.id','left')
->join('ea_push_ids t', 'e.id_users_customer = t.user_id','left')
//->join('ea_cellcarrier cc', 'u1.id_cellcarrier = cc.id','left')
->where('e.start_datetime >',$day_start)
->where('e.start_datetime <',$day_end)
->get()->result();
}


My problem is that there are multiple push_identifiers (ea_push_ids), but this query only retrieves one. How can I retrieve all the push identifiers belonging to a certain user without messing up the rest of the query?

Answer

You can use GROUP_CONCAT to put all the ids into a comma - delimited string. (Be aware: I changed the alias to push_identifiers with an s)

public function get_days_appointments($day) {
    $day_start = date('Y-m-d 00:00:00', $day);
    $day_end = date('Y-m-d 23:59:59', $day);
        return $this->db->select('e.id, 
            e.start_datetime, 
            e.end_datetime, 
            e.hash, 
            e.id_google_calendar, 
            u1.first_name AS customer_first_name, 
            u1.last_name AS customer_last_name, 
            u1.email AS customer_email, 
            u1.phone_number AS customer_phone_number, 
            u2.first_name AS provider_first_name, 
            u2.last_name AS provider_last_name, 
            u2.email AS provider_email, 
            u2.phone_number AS provider_phone_number, 
            s.name, 
            s.duration, 
            s.price, 
            s.description,
            t.push_identifiers')
        ->from('ea_appointments e')
        ->join('ea_users u1', 'e.id_users_customer = u1.id','left') 
        ->join('ea_users u2', 'e.id_users_provider = u2.id','left') 
        ->join('ea_services s', 'e.id_services = s.id','left')
        ->join('(SELECT GROUP_CONCAT(push_identifier) AS push_identifiers, user_id 
         FROM ea_push_ids
         GROUP BY user_id) t', 'e.id_users_customer = t.user_id','left')
        ->where('e.start_datetime >',$day_start)
        ->where('e.start_datetime <',$day_end)
    ->get()->result();
}

Or like this

public function get_days_appointments($day) {
    $day_start = date('Y-m-d 00:00:00', $day);
    $day_end = date('Y-m-d 23:59:59', $day);
        return $this->db->select('e.id, 
            e.start_datetime, 
            e.end_datetime, 
            e.hash, 
            e.id_google_calendar, 
            u1.first_name AS customer_first_name, 
            u1.last_name AS customer_last_name, 
            u1.email AS customer_email, 
            u1.phone_number AS customer_phone_number, 
            u2.first_name AS provider_first_name, 
            u2.last_name AS provider_last_name, 
            u2.email AS provider_email, 
            u2.phone_number AS provider_phone_number, 
            s.name, 
            s.duration, 
            s.price, 
            s.description,
            (SELECT GROUP_CONCAT(t.push_identifier) 
         FROM ea_push_ids t
         WHERE t.user_id = e.id_users_customer) AS push_identifiers')
        ->from('ea_appointments e')
        ->join('ea_users u1', 'e.id_users_customer = u1.id','left') 
        ->join('ea_users u2', 'e.id_users_provider = u2.id','left') 
        ->join('ea_services s', 'e.id_services = s.id','left')
        ->where('e.start_datetime >',$day_start)
        ->where('e.start_datetime <',$day_end)
    ->get()->result();
}