【问题标题】:SQL - AND gets ignored after Like STATEMENTSQL - AND 在 Like STATEMENT 之后被忽略
【发布时间】:2014-03-20 13:56:53
【问题描述】:
SELECT * FROM cars WHERE carBrand LIKE '%alfa%' OR carModel LIKE '%alfa%'

我在那句话之后的所有内容都会被忽略。 我希望能够做到这一点。

SELECT * FROM cars
WHERE carBrand LIKE '%alfa%'
OR carModel LIKE '%alfa%' AND carType = 'truck'

这只是被忽略了。

AND carType = 'truck'

为什么?

【问题讨论】:

  • 我要尝试的第一件事是SELECT * FROM cars WHERE (carBrand LIKE '%alfa%') OR (carModel LIKE '%alfa%' AND carType = 'truck')

标签: php sql sql-like


【解决方案1】:

使用括号

SELECT * FROM cars 
WHERE (carBrand LIKE '%alfa%' OR carModel LIKE '%alfa%')
AND carType = 'truck'

AND 的绑定比OR 强。它被称为运算符优先级。如果没有括号,您的查询将编译为

SELECT * FROM cars 
WHERE carBrand LIKE '%alfa%' 
OR (carModel LIKE '%alfa%' AND carType = 'truck')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-17
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多