什么是sql server中的'where 1 = 1'和'where null = null' [英] What is 'where 1=1' and 'where null=null' in sql server

查看:86
本文介绍了什么是sql server中的'where 1 = 1'和'where null = null'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些名为学生的列表。



我使用以下三个查询来选择行。



1.

I have a table of some columns named as Students.

I am using following three queries to select rows.

1.

select * from Students



2.


2.

select * from Students where 1=1



3.


3.

select * from Students where null=null



当我先执行然后返回所有行。



当我执行第二行时,这也会返回所有行。



但是当我执行第三个然后这不会返回任何一行。



我有两个问题



什么是1 = 1且null = null我们为什么要使用这些?

fir之间有什么区别st查询和第二次查询?


When I execute first then this returns all rows.

When I execute second then this is also returns all rows.

But when I execute third then this does not return any row.

I have two issues

What is 1=1 and null=null and why are we using these?
What is difference between first query and second query?

推荐答案

SELECT ... WHERE 1=1

是一个毫无意义的浪费: 1 = 1 评估为真,所以所有记录都将被退回。它在功能上与没有where子句的相同select相同(但是更难以阅读,并且可能因为必须解析命令而效率稍低)



is a pointless waste: 1=1 evaluates to true, so all records will be returned. It's functionally identical to the same select without the where clause (but harder to read, and probably slightly less efficient as the command has to be parsed)

SELECT ... WHERE null=null

不同: null在SQL中传播,因为它们不是真实值 - 只是一个不存在的值的标记 - 所以任何带有 null 的表达式返回。由于 null 不为真,WHERE子句总是失败,并且不返回任何行。



见这里: http://en.wikipedia.org/wiki/Null_(SQL) [ ^ ]

is different: nulls propagate in SQL because they aren't "real" values - just a marker for a non-existent value - so any expression with a null in it, returns null. Since null is not true, the WHERE clause always fails, and no rows are returned.

See here: http://en.wikipedia.org/wiki/Null_(SQL)[^]


这会有效。比较NULL时,您应始终使用IS

This would work though. When comparing NULL you should always use "IS"
where null is null

这篇关于什么是sql server中的'where 1 = 1'和'where null = null'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆