为什么此查询返回的用户不是在提供的日期出生?

Cou*_*ney 2 precedence

我正在尝试使用姓名和出生日期搜索客户。我的查询包含在下面。

Select * 
From Client
Inner Join Address
On Client.num = Address.cl_num
Where 
   Client.fname LIKE ‘%john%’ and Client.sname LIKE ‘%doe%’
Or
    Client.fname LIKE ‘%doe%’ and Client.sname LIKE ‘%john%’
And Client.year = 1973
And Client.month = 06
And Client.day = 05
Run Code Online (Sandbox Code Playgroud)

然而,结果显示的客户不是那天出生的。我的说法有问题吗?

该查询在 iSeries 服务器上运行。

jam*_*man 7

OR条款使您的选择短路。OR绑定低于AND. 使用括号将该条件分组如下:

Select * 
From Client
Inner Join Address
On Client.num = Address.cl_num
Where 
(
Client.fname LIKE ‘%john%’ and Client.sname LIKE ‘%doe%’
Or
Client.fname LIKE ‘%doe%’ and Client.sname LIKE ‘%john%’
)
And Client.year = 1973
And Client.month = 06
And Client.day = 05
Run Code Online (Sandbox Code Playgroud)

所有语言中都存在运算符的优先规则。例如,在大多数编程语言中,3 + 5 * 2 将为您提供 13 而不是 16。 *具有更高的优先级+,同样的方式AND比 具有更高的优先级OR。如果您希望加法,3+5发生在乘法之前,您将使用括号:(3 + 5) * 2