根据第一个表mysql中的列值连接带有条件的多个表

时间:2017-05-11 10:59:42

标签: php mysql sql join codeigniter-3

嗨,我有八个表,我必须从这些表中获取数据。表结构如下:

event_info
id | event_id | event_types | animal_type

human_victim_info
id | event_id | victim_full_name | address

livestock_destruction_info
id | event_id | owner_name | owner_address

crop_destruction_info
id | event_id | crop_land_type

proprty_destruction_info
id | event_id | property_type | cost

other_destruction_info
id | event_id | description

additional_info
id | event_id | images | feedback

wild_animal_info 
id | event_id | number | location_from | location_to

我使用了join作为:

SELECT e.*
     , wa.*
     , hv.*
     , lv.*
     , c.*
     , p.*
     , o.*
     , a.*
     , e.id as eId
     , e.event_id as eventId 
  FROM event_info e
  LEFT 
  JOIN wild_animal_info wa 
    ON wa.event_id = e.event_id 
  LEFT 
  JOIN human_victim_info hv 
    ON hv.event_id = e.event_id
   AND e.event_types LIKE '%1%'
  LEFT 
  JOIN livestock_destruction_info lv 
    ON lv.event_id = e.event_id 
   AND e.event_types LIKE '%2%'
  LEFT 
  JOIN crop_destruction_info c 
    ON c.event_id = e.event_id 
   AND e.event_types LIKE '%3%'
  LEFT 
  JOIN proprty_destruction_info p 
    ON p.event_id = e.event_id 
   AND e.event_types LIKE '%4%' 
  LEFT 
  JOIN other_destruction_info o 
    ON o.event_id = e.event_id 
   AND e.event_types LIKE '%5%'
  LEFT 
  JOIN additional_info a 
    ON a.event_id = e.event_id 
 WHERE e.event_id LIKE '%0026-%'; 

只有当event_info表中的event_types包含1时,我才想加入human_victim_info表,类似地,只有当event_info表中的event_types包含2时才加入livestock_destruction_info,类似地,只有当event_info表中的event_types包含3时才加入crop_destruction_info,依此类推至other_destruction信息。但我必须始终加入wild_animal_info和additional_info_table。我在每张桌子上都有event_id。我怎么能这样做?

0 个答案:

没有答案