默认情况下,SQL选择非空值

时间:2015-06-01 11:10:02

标签: sql oracle

我需要一个sql语句,当字段不为null时可能会选择行,但是如果每个字段都为null,请显示它们。

类似的东西:

import com.image.SlidingLayer;

示例:

有一个像这样的表:

SELECT
field1, field2, field3
FROM
tableName
WHERE field1 = 
CASE WHEN field1 IS NOT NULL THEN 
    field1
ELSE
    null
END;

结果将是:

 ______________________________________
|   field1   |   field2   |   field3   |
 --------------------------------------
|    null    |  TestData  |    123     |
|     23     |  TestData  |    123     |
|    null    |  TestData  |    123     |
 --------------------------------------

但是如果每个field1的数据为null:

 ______________________________________
|   field1   |   field2   |   field3   |
 --------------------------------------
|     23     |  TestData  |    123     |
 --------------------------------------

它应显示每一行

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT
field1, field2, field3
FROM
tableName
WHERE (field1 is not null) or 
(field1 is null and field2 is null and field3 is null)

答案 1 :(得分:0)

有数字:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE test ( field1, field2, field3 ) AS
          SELECT null, 'TestData', 123 FROM DUAL
UNION ALL SELECT 23,   'TestData', 123 FROM DUAL
UNION ALL SELECT null, 'TestData', 123 FROM DUAL;

查询1

SELECT field1,
       field2,
       field3
FROM   test
WHERE  NOT EXISTS ( SELECT 1
                    FROM   test
                    WHERE  field1 IS NOT NULL )
OR     field1 IS NOT NULL

<强> Results

| FIELD1 |   FIELD2 | FIELD3 |
|--------|----------|--------|
|     23 | TestData |    123 |

仅使用空值

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE test ( field1, field2, field3 ) AS
          SELECT CAST( null AS NUMBER(4) ), 'TestData', 123 FROM DUAL
UNION ALL SELECT null,   'TestData', 123 FROM DUAL
UNION ALL SELECT null, 'TestData', 123 FROM DUAL;

查询2

SELECT field1,
       field2,
       field3
FROM   test
WHERE  NOT EXISTS ( SELECT 1
                    FROM   test
                    WHERE  field1 IS NOT NULL )
OR     field1 IS NOT NULL

<强> Results

| FIELD1 |   FIELD2 | FIELD3 |
|--------|----------|--------|
| (null) | TestData |    123 |
| (null) | TestData |    123 |
| (null) | TestData |    123 |

<强> -------- --------编辑

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE test ( field1, field2, field3 ) AS
          SELECT null, 'TestData', 123 FROM DUAL
UNION ALL SELECT 23,   'TestData', 124 FROM DUAL
UNION ALL SELECT null,   'TestData', 124 FROM DUAL
UNION ALL SELECT null, 'TestData', 125 FROM DUAL;

查询1

SELECT field1,
       field2,
       field3
FROM   test
WHERE  (
         NOT EXISTS ( SELECT 1
                      FROM   test
                      WHERE  field1 IS NOT NULL
                      AND    field2 = 'TestData'
                      AND    field3 = 124
                    )
       OR field1 IS NOT NULL
       )
AND field2 = 'TestData'
AND field3 = 124

<强> Results

| FIELD1 |   FIELD2 | FIELD3 |
|--------|----------|--------|
|     23 | TestData |    124 |

查询2

SELECT field1,
       field2,
       field3
FROM   test
WHERE  (
         NOT EXISTS ( SELECT 1
                      FROM   test
                      WHERE  field1 IS NOT NULL
                      AND    field2 = 'TestData'
                      AND    field3 = 125
                    )
       OR field1 IS NOT NULL
       )
AND field2 = 'TestData'
AND field3 = 125

<强> Results

| FIELD1 |   FIELD2 | FIELD3 |
|--------|----------|--------|
| (null) | TestData |    125 |