繁体   English   中英

我的WHERE子句条件中存在问题。 <>和AND或

[英]Having issues inside my WHERE clause conditions. <> and AND OR

我正在研究锻炼生成类型的应用程序,而mysql查询出现问题。 我遇到的问题是,当我在where子句中将它们特别指定为不等于<>时,我会不断抓取多个特定的ExerciseID。

SELECT `Exercise_Name` , `Body_Part_Focus` , `Video_FileName` , `Alignment_Body_Position` ,  `Description_Movement_Technique` , `Tip1_Trainer_Tips` , `Sets` , `Post_Season` , `Off_Season`, `Pre_Season` , `In_Season` , `Measurement_Type` , `Measurement_Type_Label` ,`ExerciseID` 
FROM `tbl_exercises` WHERE 
`exrcs_age_approp` = '14 to 17' 
AND `exrcs_prgssn_lvl` = 'I' OR `exrcs_prgssn_lvl` = 'II' 
AND `Athletic_Skill` = 'ROM' OR `Exercise_Subskill` = 'ROM' 
AND `Body_Part_Focus` = 'Torso' 
AND `ExerciseID` <> 89 
AND `ExerciseID` <> 58 
AND `ExerciseID` <> 178 
AND `ExerciseID` <> 249 
ORDER BY RAND( ) LIMIT 0 , 1

我在做AND OR子句是否正确? 我想说的是这个和这个或这个..等..但是我认为我的mysql可能也在解释那个错误...我通常不接触后端的东西。

谢谢

这可能是由于您的ANDOR条件之间缺少括号。 我使用INNOT IN简化了您的查询:

SELECT `Exercise_Name` , `Body_Part_Focus` , `Video_FileName` , `Alignment_Body_Position` ,  `Description_Movement_Technique` , `Tip1_Trainer_Tips` , `Sets` , `Post_Season` , `Off_Season`, `Pre_Season` , `In_Season` , `Measurement_Type` , `Measurement_Type_Label` ,`ExerciseID` 
FROM `tbl_exercises`
WHERE `exrcs_age_approp` = '14 to 17' 
AND `exrcs_prgssn_lvl` IN ('I', 'II')
AND (`Athletic_Skill` = 'ROM' OR `Exercise_Subskill` = 'ROM')
AND `Body_Part_Focus` = 'Torso' 
AND `ExerciseID` NOT IN (89, 58, 178, 249) 
ORDER BY RAND( ) LIMIT 0 , 1

您需要在WHERE子句中为逻辑添加括号。 我会建议:

WHERE `exrcs_age_approp` = '14 to 17' AND
      `exrcs_prgssn_lvl` IN ('I', 'II') AND
      (`Athletic_Skill` = 'ROM' OR `Exercise_Subskill` = 'ROM') AND
      `Body_Part_Focus` = 'Torso' AND
      `ExerciseID` NOT IN (89, 58, 178, 249)

INNOT IN也应有助于逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM