MySQL连接中的多行

时间:2016-07-22 11:15:26

标签: php mysql

我有这个查询/ PHP代码:

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();
}

我的问题是有多个push_identifier(ea_push_ids),但此查询只检索一个。如何在不弄乱查询的其余部分的情况下检索属于某个用户的所有推送标识符?

1 个答案:

答案 0 :(得分:1)

您可以使用GROUP_CONCAT将所有ID放入以逗号分隔的字符串中。 (请注意:我已使用push_identifiers

将别名更改为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();
}

或者像这样

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();
}