在sqlite3中执行INSERT CASE时出现“数据类型不匹配”

时间:2016-11-19 08:59:45

标签: sqlite

我有两个表,一个填充了数据

CREATE TABLE `tableX` (`column1`    REAL);

INSERT INTO tableX VALUES (5);
INSERT INTO tableX VALUES (-3);

CREATE TABLE `tableY` 
(`rowid` INTEGER PRIMARY KEY, `column2` REAL);

INSERT CASE语句是

INSERT INTO tableY SELECT column1, 
CASE WHEN column1 >=0 THEN 1 ELSE 2 END as column2 FROM tableX;

错误是我的数据库浏览器中的“数据类型不匹配”,但似乎在sqlfiddle中工作...至少没有错误

非常感谢任何帮助!谢谢:D!

1 个答案:

答案 0 :(得分:1)

不是最优雅的解决方案,但它是一种解决方法。

CREATE TABLE `tableX` (`column1`    REAL);
INSERT INTO tableX VALUES (5);
INSERT INTO tableX VALUES (-3);

CREATE TABLE `tableZ` (`column1` REAL, `column3` REAL);
INSERT INTO tableZ SELECT column1, 
CASE WHEN column1 >=0 THEN 1 ELSE 2 END as column3 FROM tableX;

CREATE TABLE 'tableY' (`rowid` INTEGER PRIMARY KEY, `column2` REAL)
INSERT INTO tableY (column2) SELECT column3 FROM tableZ;

DROP TABLE tableZ;