我希望以下查询根据specimen.snop_axis
的值进行更改,该值可以是“M”或“F”:
SELECT *
FROM specimen,
topography_index,
morphology,
FUNCTION
WHERE SUBSTRING(specimen.topography_index, 2, 2) = topography_index.topography_index_code
AND morphology.morphology_code = specimen.snop_code
AND specimen.topography_index = '_ORGAN_'
因此,如果samples.snop_axis == M则添加
AND morphology.morphology_code = specimen.snop_code
否则如果samples.snop_axis == F则添加
AND functions.function_code = specimen.snop_code
更新
我已根据建议将查询更改为:
SELECT * FROM specimen, topography_index, morphology, functions
WHERE
SUBSTRING(specimen.topography_index, 2, 2) = topography_index.topography_index_code
AND
IF(specimen.snop_axis == 'M', morphology.morphology_code = specimen.snop_code, functions.function_code = specimen.snop_code)
AND
specimen.topography_index = '_ORGAN_'
但是我收到了一个错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== 'M', morphology.morphology_code = specimen.snop_code, functions.function_code' at line 1
答案 0 :(得分:1)
试试这个。
SELECT *
FROM specimen,
topography_index,
morphology,
FUNCTION
WHERE SUBSTRING(specimen.topography_index, 2, 2) = topography_index.topography_index_code
AND specimen.topography_index = '_ORGAN_'
AND if(specimen.snop_axis = 'M', morphology.morphology_code = specimen.snop_code, functions.function_code = specimen.snop_code);
答案 1 :(得分:1)
你可以简单地使用这样的case语句: -
SELECT * FROM specimen, topography_index, morphology, function WHERE SUBSTRING(specimen.topography_index, 2, 2) = topography_index.topography_index_code
AND morphology.morphology_code = specimen.snop_code
AND specimen.topography_index = '_ORGAN_'
AND CASE WHEN specimen.snop_axis = M THEN morphology.morphology_code = specimen.snop_code
ELSE function.function_code = specimen.snop_code END;
这可能会对你有帮助。
答案 2 :(得分:1)
您可以使用逻辑操作AND
和OR
将它们作为条件子句,如下所示:
SELECT *
FROM specimen,
topography_index,
morphology,
FUNCTION
WHERE SUBSTRING(specimen.topography_index, 2, 2) = topography_index.topography_index_code
AND morphology.morphology_code = specimen.snop_code
AND specimen.topography_index = '_ORGAN_'
AND (
(specimen.snop_axis = 'M' AND morphology.morphology_code = specimen.snop_code)
OR
(specimen.snop_axis = 'F' AND functions.function_code = specimen.snop_code)
);
答案 3 :(得分:1)
SELECT *
FROM specimen,
topography_index,
morphology,
FUNCTION
WHERE SUBSTRING(specimen.topography_index, 2, 2) = topography_index.topography_index_code
AND morphology.morphology_code = specimen.snop_code
AND specimen.topography_index = '_ORGAN_'
AND (if(specimen.snop_axis = 'M',
morphology.morphology_code = specimen.snop_code,
functions.function_code = specimen.snop_code)
OR if(specimen.snop_axis = 'F',functions.function_code = specimen.snop_code));