从字段与条件不匹配的表中选择
我只是想知道我可以执行什么样的 SQL 命令来从某个表中选择所有项目,其中 A 列不等于 x 且 B 列不等于 x代码>
类似:
select something from table
where columna does not equal x and columnb does not equal x
有什么想法吗?
I'm just wondering what kind of SQL command I could execute that would select all items from a certain table where column A is not equal to x
and column B is not equal to x
Something like:
select something from table
where columna does not equal x and columnb does not equal x
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
关键是 sql 查询,您将其设置为字符串:
请注意,有很多方法可以指定 NOT。另一种同样有效的方法是:
这是如何使用它的完整示例:
您可以在上面的 while 循环中执行您想做的任何操作。将表中的每个字段作为
$row array
的元素进行访问,这意味着$row['field1']
将为您提供field1
的值code> 位于当前行,$row['field2']
将为您提供field2
的值。请注意,如果列可能具有
NULL
值,则使用上述任一语法都无法找到这些值。您需要添加子句以包含NULL
值:The key is the sql query, which you will set up as a string:
Note that there are a lot of ways to specify NOT. Another one that works just as well is:
Here is a full example of how to use it:
You can do whatever you would like within the above while loop. Access each field of the table as an element of the
$row array
which means that$row['field1']
will give you the value forfield1
on the current row, and$row['field2']
will give you the value forfield2
.Note that if the column(s) could have
NULL
values, those will not be found using either of the above syntaxes. You will need to add clauses to includeNULL
values:您可以使用像
Or
Or
和像 Jeffly Bake 的查询一样,对于包含 null 值,您不必这样写
简化它
您可以通过<=> 来 是 Null Safe 等于运算符,其中甚至包括空值的结果。
You can use like
Or
Or
And like Jeffly Bake's query, for including null values, you don't have to write like
You can make it simple by
<=> is the Null Safe equal to Operator, which includes results from even null values.
或者也可以将语句插入括号内。
Or can also insert the statement inside bracket.
//此工作以不区分大小写的方式进行
//this work in case insensitive manner
我建议使用菱形运算符 (<>) 来代替 !=,因为第一个是有效的 SQL,第二个是 MySQL 添加。
I'd suggest using the diamond operator (<>) in favor of != as the first one is valid SQL and the second one is a MySQL addition.
如果存在空值,上述答案将不起作用。
下面是使用合并来评估可以为 null 的字段的简写:
这里我们告诉查询将 NULL 视为 0,而不为 null 添加完全其他的条件,这将使接线简单并且代码易于阅读。
Above answers will not work if there's a null value.
Here's a shorthand to evaluate fields that can be null using coalesce:
Here we tell the query to treat NULL as a 0, without adding a completely other condition for nulls, which would keep wiring simple and the code easily readable.
您还可以使用
You can use also