mysql concat连接结果集的多行

时间:2019-05-20 20:36:51

标签: mysql codeigniter

这是我正在处理的Codeigniter的活动记录查询

$this->db->select('wo.*,
            en.equipment_name as conventional_name,
            cat.name as category_name,
            toe.name as type_of_equipment_text,
            wos.wo_status,
            l.name as location_name, spl.name as supplier_name,
            ht.shortcode as type_code, c.name as city_name, 
            dt.name as district_name, d.name as division_name, z.name as zone_name')
             ->from('work_orders wo')
             ->join(DB_PREFIX . 'equipment e', 'e.ID = wo.tag_num', 'left')
             ->join(DB_PREFIX . 'equipment_names en', 'en.ID = e.conventional_name', 'left')
             ->join(DB_PREFIX . 'categories cat', 'cat.ID = en.category_id', 'left')
             ->join(DB_PREFIX . 'equipment_type toe', 'toe.ID = e.type_of_equipment', 'left')
             ->join(DB_PREFIX . 'suppliers spl', 'spl.ID = e.purchased_from_supplier_name AND spl.status=1', 'left')
             ->join(DB_PREFIX . 'locations l', 'l.ID = e.city_id', 'left')
             ->join(DB_PREFIX . 'hospital_type ht', 'ht.ID = l.type_id', 'left')
             ->join(DB_PREFIX . 'cities c', 'c.ID = l.city_id', 'left')
             ->join(DB_PREFIX . 'districts dt', 'dt.ID = c.district_id', 'left')
             ->join(DB_PREFIX . 'divisions d', 'd.ID = dt.division_id', 'left')          
             ->join('work_orders_status wos', 'wos.id = wo.wo_status', 'left')
             ->join(DB_PREFIX . 'zones z', 'z.ID = d.zone_id', 'left')
             ->where("find_in_set($get_engineer_id, wo.`assigned_to_enggs`)");

work_orders表中,我有一列assigned_to_enggs,其中每条记录的员工ID用逗号分隔。我想通过与employees表连接来获得用逗号分隔的逗号分隔的雇员姓名。

这是我的work_orders表

╔══════════╦═══════════════════╗
║ ID       ║assigned_to_enggs  ║
╠══════════╬═══════════════════╣
║ 1        ║ 2,4,5             ║
║ 2        ║ 1,2               ║
║ 3        ║ 2                 ║
║ 4        ║ 3,1               ║
║ 5        ║ 2,4,1             ║
╚══════════╩═══════════════════╝

员工表

╔══════════╦═════════════════╗
║ emp_id   ║    name         ║
╠══════════╬═════════════════╣
║ 1        ║ Irfan           ║
║ 2        ║ Kamran Khan     ║
║ 3        ║ Faiza           ║
║ 4        ║ Atif            ║
║ 5        ║ Farrukh         ║
╚══════════╩═════════════════╝

我尝试过GROUP_CONCAT(),但对我没有用。有人能帮我吗。预先感谢

0 个答案:

没有答案