MySQL-根据字段值修改查询
我希望根据sample.snop_轴的值(可以是'M'或'F')更改以下查询:MySQL-根据字段值修改查询,mysql,Mysql,我希望根据sample.snop_轴的值(可以是'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 = specime
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_'
因此,如果sample.snop_轴==M,则添加
和morphics.morphics_代码=sample.snop_代码
否则,如果sample.snop_轴==F,则添加
AND functions.function_code=sample.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
试试这个
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);
您可以像这样简单地使用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;
这可能会对您有所帮助。您可以使用逻辑运算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)
);
谢谢,但我得到了一个错误…请参阅OPSorry中的changed query,这是因为==,在mysql中,检查相等性是=。嗯…应该可以,但它返回了同一样本的2292条记录。snop_代码…表形态中有2292条记录…您正在连接4个表,您正在执行*这意味着它将返回所有4个表中的所有列。指定所需的列以及这些表之间的关系。
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));