gpt4 book ai didi

mysql - SQL WHERE 条件,为什么使用 'a != 7 and b !=836' 的结果不等于 ' !(a=7 and b=836)' ?

转载 作者:行者123 更新时间:2023-11-29 10:06:13 29 4
gpt4 key购买 nike

有如下三个记录:

  • a=7,b=836
  • a=8,b=836
  • a=7,b=839

我想得到没有(a=7和b=836)的结果。

执行select * from test where a!=7 and b!=836这样的sql,结果为空,但使用 select * from test where a<>7 or b<>836 得到了正确的结果。

我有两个问题:

  1. 为什么“a!=7 和 b!=836”不等于!(a=7 和 b=836)?
  2. 这两种情况有什么不同?“a!=7 和 b!=836”和“a<>7 或 b<>836”

最佳答案

区别在于逻辑运算符 andor ,与括号结合。

select * from test where a!=7 and b!=836

此查询将选择两个语句都返回 true 的位置,即 a 不为 7,b 不为 836。这不会返回任何内容,没有这两个语句都为 true 的记录。

select * from test where !(a=7 and b=836)

当您将 and 括在括号中时,你可以移动逻辑。该语句意味着所有记录都不匹配 a 为 7 且 b 为 836 的任何记录。因此在括号内它与第一条记录匹配,然后反转该选择。

select * from test where a<>7 or b<>836

<>!= 相同(Link to documentation) 。但在此查询中您使用 or 。因此它将匹配 a 不为 7 的任何记录以及不为 836 的任何记录。因此将匹配第二行和第三行。

如需更多阅读 Material ,请查看 De Morgan's Laws!(a and b)等于 !a or !b 。更多说明herehere .

关于mysql - SQL WHERE 条件,为什么使用 'a != 7 and b !=836' 的结果不等于 ' !(a=7 and b=836)' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51855542/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com