我正在使用锻炼生成类型的应用程序,而且我遇到了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也可能解释错误...我通常不会触及后端的东西。
谢谢
答案 0 :(得分:2)
这可能是由于AND
和OR
条件之间缺少括号。我使用IN
和NOT 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)
IN
和NOT IN
也应该有助于逻辑。