我的查询是由codeignter生成的:
SELECT `games_link`.`APPID`, `games_other`.`name`, `games_other`.`logo`, `platforms`.`PID`, `platforms`.`name` AS pname, `platforms`.`logo` AS plogo
FROM (`games_link`)
LEFT JOIN `games_platforms` ON `games_platforms`.`APPID` = `games_link`.`APPID`
LEFT JOIN `platforms` ON `platforms`.`PID` = `games_platforms`.`PID`
LEFT JOIN `games_other` ON `games_other`.`APPID` = `games_link`.`GB_ID`
WHERE `games_platforms`.`PID` = '94'
OR `games_platforms`.`PID` = '139'
OR `games_platforms`.`PID` = '35'
OR `games_platforms`.`PID` = '129'
OR `games_platforms`.`PID` = '146'
OR `games_platforms`.`PID` = '20'
OR `games_platforms`.`PID` = '145'
AND `games_other`.`name` LIKE '%knack%' LIMIT 15
当它运行时它不会通过LIKE过滤,它只返回所有内容,但是如果我运行它:
SELECT `games_link`.`APPID`, `games_other`.`name`, `games_other`.`logo`, `platforms`.`PID`, `platforms`.`name` AS pname, `platforms`.`logo` AS plogo
FROM (`games_link`)
LEFT JOIN `games_platforms` ON `games_platforms`.`APPID` = `games_link`.`APPID`
LEFT JOIN `platforms` ON `platforms`.`PID` = `games_platforms`.`PID`
LEFT JOIN `games_other` ON `games_other`.`APPID` = `games_link`.`GB_ID`
WHERE `games_other`.`name` LIKE '%knack%' LIMIT 15
它运行LIKE。
答案 0 :(得分:4)
据推测,您打算将任何游戏类型与and
结合使用。但是,您的逻辑不正确,因为您没有括号。解决此问题的最简单方法是使用in
:
WHERE `games_platforms`.`PID` in (94, 139, 35, 129, 146, 146, 20, 145) and
`games_other`.`name` LIKE '%knack%' ;
将来,请在where
子句中围绕条件使用括号,直到您完全理解or
和and
的优先规则。
答案 1 :(得分:0)
由于您说无法按照Gordon Linoff的建议在in clause
生成查询,请尝试以下方法:
WHERE
( -----> Open paranthesis here
`games_platforms`.`PID` = '94'
OR `games_platforms`.`PID` = '139'
OR `games_platforms`.`PID` = '35'
OR `games_platforms`.`PID` = '129'
OR `games_platforms`.`PID` = '146'
OR `games_platforms`.`PID` = '20'
OR `games_platforms`.`PID` = '145'
) -----> Close paranthesis here
AND `games_other`.`name` LIKE '%knack%' LIMIT 15