【问题标题】:WHERE OR stops LIKE from workingWHERE OR 停止 LIKE 工作
【发布时间】:2014-09-28 01:25:19
【问题描述】:

我有这个查询是由 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。

【问题讨论】:

  • 您可能需要考虑阅读这些twoquestions
  • 在查询中使用大括号 ( cond1 or cond2 or cond3) and cond4。你的系统有可能吗?

标签: php mysql sql codeigniter


【解决方案1】:

大概您打算将任何游戏类型与and 结合使用。但是,您的逻辑不正确,因为您没有括号。最简单的修复方法是使用in

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

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

【讨论】:

  • 这是由 codeignters 活动记录生成的。所以真的不能轻易避免这样的事情。
  • 你看到Maxime Morin对你原帖的评论了吗?我想这会对你有所帮助。
  • 在此之前我已经修复了它:->where('games_platforms.PID IN (94,'.implode(",", $plat).')')
【解决方案2】:

既然您说不可能按照 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2012-11-06
    • 2014-05-22
    • 2019-12-06
    • 2020-10-05
    相关资源
    最近更新 更多