Sqlite 带条件的SQL IFNULL并写入CSV
我有一个空值的表,所有列的数据类型都是整数/实数。 如果将数据写入csv时字段为空,我希望写入“无数据” 这是测试表Sqlite 带条件的SQL IFNULL并写入CSV,sqlite,ifnull,Sqlite,Ifnull,我有一个空值的表,所有列的数据类型都是整数/实数。 如果将数据写入csv时字段为空,我希望写入“无数据” 这是测试表 id test1 test2 test3 ------ ------ ------ ------ 1 1 2 3 2 5 6 3 7 9 4 11 12 id test1 test2 test3 ------ ------ -
id test1 test2 test3
------ ------ ------ ------
1 1 2 3
2 5 6
3 7 9
4 11 12
id test1 test2 test3
------ ------ ------ ------
1 1 2 3
2 string 5 6
3 7 9
4 11 12
我只想在列test1中显示'string',其中id为2。
我的sql语句是
从testTable中选择id,(ifnull(test1,'string'))作为test1、test2、test3代码>
它产生了这个
id test1 test2 test3
------ ------ ------ ------
1 1 2 3
2 string 5 6
3 7 9
4 string 11 12
有没有一种方法可以为ifnull放置条件子句,或者有没有另一种方法可以生成这个最终的表
我想要的最终测试表
id test1 test2 test3
------ ------ ------ ------
1 1 2 3
2 5 6
3 7 9
4 11 12
id test1 test2 test3
------ ------ ------ ------
1 1 2 3
2 string 5 6
3 7 9
4 11 12
谢谢。当id为2时,您可以使用相关子查询并仅拉入“字符串”:
SELECT id, ifnull(test1,
(select 'string' from testTable tt
where tt.id =2 and testTable.id = tt.id)) as test1,
test2, test3
from testTable;
也可以使用CASE语句来完成,如果有大量数据,那么CASE语句可能会更干净、更快
SELECT id,
CASE WHEN test1 IS NULL AND id = 2 THEN 'string' ELSE test1 END,
test2,
test3
from testTable;
我不知道你可以在那里放一个子查询。非常感谢。啊,我在前面使用case语句时,看着你们的代码,我完全错了。再次感谢!