我有以下查询,希望使用IF
条件进行修改:
if(specimen.snop_axis = 'M', join morphology
on morphology.morphology_code = specimen.snop_code, join functions on functions.functions_code = specimen.snop_code)
select *
from specimen
join topography_index
on substring(specimen.topography_index, 2, 2) =
topography_index.topography_index_code
join morphology
on morphology.morphology_code = specimen.snop_code
join functions
on functions.functions_code = specimen.snop_code
left join specimen_image_lookup
on specimen_image_lookup.specimen_fk = specimen.specimen_pk
left join image
on image.image_pk = specimen_image_lookup.image_fk
where specimen.specimen_pk = '$specimen'
如果条件该如何修改此查询以合并此查询?也就是说,IF语句需要替换:
join morphology
on morphology.morphology_code = specimen.snop_code
join functions
on functions.functions_code = specimen.snop_code
最佳答案
在两个表的连接条件中包括specimen.snop_axis
的逻辑(等于或不等于),如下所示:
SELECT
*
FROM specimen
JOIN topography_index
ON SUBSTRING(specimen.topography_index, 2, 2) =
topography_index.topography_index_code
LEFT JOIN morphology
ON specimen.snop_axis = 'M' AND morphology.morphology_code = specimen.snop_code
LEFT JOIN functions
ON specimen.snop_axis <>'M' AND functions.functions_code = specimen.snop_code
LEFT JOIN specimen_image_lookup
ON specimen_image_lookup.specimen_fk = specimen.specimen_pk
LEFT JOIN image
ON image.image_pk = specimen_image_lookup.image_fk
WHERE specimen.specimen_pk = '$specimen'
;
关于mysql - MySQL-具有IF条件的联接表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26091845/