SQLite 选择哪里为空?

在 SQLite 中,如何选择 some_column为空的记录?
空则同时作为 NULL""

156846 次浏览

看起来你只需要做:

SELECT * FROM your_table WHERE some_column IS NULL OR some_column = '';

测试案例:

CREATE TABLE your_table (id int, some_column varchar(10));


INSERT INTO your_table VALUES (1, NULL);
INSERT INTO your_table VALUES (2, '');
INSERT INTO your_table VALUES (3, 'test');
INSERT INTO your_table VALUES (4, 'another test');
INSERT INTO your_table VALUES (5, NULL);

结果:

SELECT id FROM your_table WHERE some_column IS NULL OR some_column = '';


id
----------
1
2
5

也许你的意思是

select x
from some_table
where some_column is null or some_column = ''

但我不知道,因为你根本没问。

有几种方法,比如:

where some_column is null or some_column = ''

或者

where ifnull(some_column, '') = ''

或者

where coalesce(some_column, '') = ''

where ifnull(length(some_column), 0) = 0

你可以这样做:

int counter = 0;
String sql = "SELECT projectName,Owner " + "FROM Project WHERE Owner= ?";
PreparedStatement prep = conn.prepareStatement(sql);
prep.setString(1, "");
ResultSet rs = prep.executeQuery();
while (rs.next()) {
counter++;
}
System.out.println(counter);

这将显示列值为空或空的行的编号。