sql 判断为空不为空
Sometimes we have columns where null values are allowed. In such cases, SQL IS NULL is a very useful operator.
有时我们有允许空值的列。 在这种情况下,SQL IS NULL是一个非常有用的运算符 。
SQL IS NULL operator is used to checking if the value of a column is null or not. The operator filters the result set based on the null column value.
SQL IS NULL运算符用于检查列的值是否为null。 操作员根据空列值过滤结果集。
Let’s discuss in detail about the IS NULL operator.
让我们详细讨论IS NULL运算符。
SQL IS NULL (SQL IS NULL)
Let’s have a quick look at SQL is null operator syntax.
让我们快速了解一下SQL是null运算符语法。
SELECT Column(s) FROM table_name WHERE column IS NULL;
Above query will give us result set where specified column value is null.
上面的查询将为我们提供结果集,其中指定的列值为null。
We will understand the above-mentioned syntax in more detail through some examples.
通过一些示例,我们将更详细地理解上述语法。
Let’s consider the following Student table for example purpose.
让我们考虑以下学生表格作为示例。
RollNo | StudentName | StudentGender | StudentAge | StudentPercent |
---|---|---|---|---|
1 | George | M | 14 | 85 |
2 | Monica | F | 12 | 88 |
3 | Jessica | F | null | 84 |
4 | Tom | M | null | 78 |
卷号 | 学生姓名 | 学生性别 | 学生年龄 | 学生百分比 |
---|---|---|---|---|
1个 | 乔治 | 中号 | 14 | 85 |
2 | 莫妮卡 | F | 12 | 88 |
3 | 杰西卡(Jessica) | F | 空值 | 84 |
4 | 汤姆 | 中号 | 空值 | 78 |
Scenario: Get the percentage of students whose age is null.
方案 :获取年龄为零的学生的百分比。
SELECT StudentPercent FROM Student WHERE StudentAge IS NULL;
Output:
输出:
StudentPercent |
---|
88 |
78 |
学生百分比 |
---|
88 |
78 |
Oracle空字符串等效于NULL (Oracle Empty String is equivalent to NULL)
Null is not just limited to the null keyword in Oracle database, in fact, the columns who have blank value also are considered as null when using IS NULL operator. Note that these columns types should be VARCHAR and not CHAR.
空值不仅限于Oracle数据库中的null关键字,实际上,使用IS NULL运算符时,具有空值的列也被视为空值。 请注意,这些列类型应为VARCHAR而不是CHAR。
Let’s consider the following Supplier table for example purpose.
让我们考虑以下供应商表作为示例。
ProductId | ProductName | SupplierName |
---|---|---|
1 | Cookie | ABC |
2 | Cheese | |
3 | Chocolate | |
4 | Jam | XDE |
产品编号 | 产品名称 | 供应商名称 |
---|---|---|
1个 | 曲奇饼 | 美国广播公司 |
2 | 起司 | |
3 | 巧克力 | |
4 | 果酱 | XDE |
Scenario: Get the name of the product where supplier name is not available.
场景 :获取没有供应商名称的产品名称。
SELECT ProductName FROM Supplier WHERE SupplierName IS NULL;
Output:
输出:
ProductName |
---|
Cheese |
Chocolate |
产品名称 |
---|
起司 |
巧克力 |
SQL不为空 (SQL IS NOT NULL)
SQL IS NOT NULL operator is used to filter the result if the column that is used in WHERE clause contains non-null values.
如果WHERE子句中使用的列包含非null值,则使用SQL IS NOT NULL运算符过滤结果。
Let’s discuss in detail about IS NOT NULL operator.
让我们详细讨论一下IS NOT NULL运算符。
Syntax:
语法 :
SELECT Column(s) FROM table_name WHERE Column IS NOT NULL;
In the syntax above the column values that are not null will be filtered for the result.
在上面的语法中,将为结果过滤不为null的列值。
Let’s consider the earlier defined Supplier table for example purpose.
让我们以先前定义的Supplier表为例。
Scenario: Get the name of the product whose supplier name is not null.
场景 :获取供应商名称不为null的产品的名称。
SELECT ProductName FROM Supplier WHERE SupplierName IS NOT NULL;
Output:
输出 :
SupplierName |
---|
Cookie |
Jam |
供应商名称 |
---|
曲奇饼 |
果酱 |
翻译自: https://www.journaldev.com/19897/sql-is-null-sql-is-not-null
sql 判断为空不为空