SQL 没有 WHERE 子句的 SELECT NOT NULL 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14257438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:05:19  来源:igfitidea点击:

SELECT NOT NULL values without WHERE clause

sqlsql-serverselectnull

提问by Hélder Gon?alves

Is there a way to select not null values without a where clause? i know it can be done like this

有没有办法在没有 where 子句的情况下选择非空值?我知道可以这样做

SELECT X, Y, Z
FROM T
WHERE X IS NOT NULL

but Im wondering if there is a way to put the null 'filter' in the select clause without using the where

但我想知道是否有一种方法可以在不使用 where 的情况下将空“过滤器”放在 select 子句中

maybe something like this?

也许是这样的?

SELECT NOT_NULL(X), Y, Z
FROM T

Ty

回答by SQLMenace

no, there is no way, if you need to filter stuff out you need a WHERE clause. That said, you can change the values returned from null to something else by using ISNULL or COALESCE

不,没有办法,如果你需要过滤掉东西,你需要一个 WHERE 子句。也就是说,您可以使用 ISNULL 或 COALESCE 将返回的值从 null 更改为其他值

回答by Lasse Edsvik

Are you looking for COALESCE?

您在寻找 COALESCE 吗?

i.e

IE

SELECT COALESCE(HasBananas, 'No') FROM Sometable

回答by digscoop

I can't see one sane reason to do this, but you could filter through a JOIN clause if you really don't want a WHERE.

我看不出这样做的合理理由,但如果您真的不想要 WHERE,您可以过滤 JOIN 子句。

SELECT X, Y, Z
FROM T
JOIN (SELECT null a) Q ON X IS NOT NULL

回答by valex

Why not :) But WHERE is MUCH better.

为什么不:) 但哪里更好。

The trick here is in JOIN ONcondition where if you compare anything with NULLit is always FALSEeven NULL=NULLis FALSE.

这里的技巧是在 JOINON条件下,如果你与NULL它比较任何东西总是FALSE偶数NULL=NULLFALSE.

SQLFiddle demo

SQLFiddle 演示

select * from t 
join 
(
select distinct x from t
) t1 on (t.x=t1.x);

回答by Ramon de Klein

If you just want to check if a value is NULL and return it as a boolean in your result, then you can use something like this:

如果您只想检查一个值是否为 NULL 并将其作为布尔值返回到您的结果中,那么您可以使用以下方法:

SELECT CASE 
         WHEN X IS NULL THEN 0
         ELSE 1
       END AS XisNull,
       Y, 
       Z
FROM T

If you just want to filter, then use the WHERE statement. It has been created for it :-)

如果您只想过滤,请使用 WHERE 语句。它是为它创建的:-)