这是我对 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 = (CASE WHEN l.ID == e.city_id THEN e.city_id ELSE wo.location)', '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');
正如您在locations
表的连接语句中看到的那样,我正在尝试实现一个 case 语句,但它对我不起作用。我想要的是如果有任何整数值,e.city_id
那么locations
表的连接语句应该变成
->join(DB_PREFIX . 'locations l', 'l.ID = e.city_id', 'left')
否则它应该变成
->join(DB_PREFIX . 'locations l', 'l.ID = wo.location', 'left')
非常感谢这种方法或替代方法的任何帮助。谢谢阅读