gpt4 book ai didi

MySQL - 使用 IF 条件连接表

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:56 25 4
gpt4 key购买 nike

我有以下查询,我想使用 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 条件?即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 - 使用 IF 条件连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26091845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com