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

时间:2014-06-14 20:19:07

标签: mysql

我正在使用锻炼生成类型的应用程序,而且我遇到了mysql查询的问题。我遇到的问题是当我特意将它们作为不等于<>时,我会继续抓取超过1个某些练习ID。在where子句中。

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也可能解释错误...我通常不会触及后端的东西。

谢谢

2 个答案:

答案 0 :(得分:2)

这可能是由于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

答案 1 :(得分:2)

您的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也应该有助于逻辑。