Answers:
有几种方法,例如:
where some_column is null or some_column = ''
要么
where ifnull(some_column, '') = ''
要么
where coalesce(some_column, '') = ''
的
where ifnull(length(some_column), 0) = 0
length
示例,因为在某些情况下它实际上可能更快,因为比较数字比比较字符串更简单。如果需要考虑性能,那么您当然应该检查一下它的功能。
看来您可以轻松做到:
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
您可以执行以下操作:
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);
这将为您提供列值为null或空白的行数。