2014-06-14 64 views
1

我正在研究鍛鍊生成類型的應用程序,並且遇到了mysql查詢的問題。我遇到的問題是當我在where子句中特別將它們作爲不等於<>時,我一直在抓取一個以上的某些ExerciseID。在我的WHERE子句條件中有問題。 <> AND AND OR

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

嘗試在OR子句中添加括號。他們的優先考慮並不是你想象的。 –

回答

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 
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也應與邏輯幫助。