在哪里或停止LIKE工作

时间:2014-08-05 11:38:20

标签: php mysql sql codeigniter

我的查询是由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

enter image description here

当它运行时它不会通过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。 enter image description here

2 个答案:

答案 0 :(得分:4)

据推测,您打算将任何游戏类型与and结合使用。但是,您的逻辑不正确,因为您没有括号。解决此问题的最简单方法是使用in

WHERE `games_platforms`.`PID` in (94, 139, 35, 129, 146, 146, 20, 145) and
      `games_other`.`name` LIKE '%knack%' ;

将来,请在where子句中围绕条件使用括号,直到您完全理解orand的优先规则。

答案 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