在HSQLDB中的JOIN操作期间用其他值替换NULL

时间:2011-11-17 17:32:06

标签: sql database hsqldb

当我使用HSQLDB 2.2.5运行以下脚本(q.sql)时,results.csv的内容将变为以下内容:

results.csv

C1,C2
[null],e
a,b

q.sql:

DROP TABLE "PUBLIC"."TABLE1" IF EXISTS;

CREATE TABLE "PUBLIC"."TABLE1" (
  "C1" VARCHAR(10),
  "C2" VARCHAR(10),
  "C3" VARCHAR(10)
);

INSERT INTO "PUBLIC"."TABLE1" ("C1", "C2", "C3") VALUES ('a', 'b', 'c');
INSERT INTO "PUBLIC"."TABLE1" ("C1", "C2", "C3") VALUES ('d', 'e', 'f');

DROP TABLE "PUBLIC"."TABLE2" IF EXISTS;

CREATE TABLE "PUBLIC"."TABLE2" (
  "C1" VARCHAR(10),
  "C2" VARCHAR(10),
);

INSERT INTO "PUBLIC"."TABLE2" ("C1", "C2") VALUES ('a', 'b');
INSERT INTO "PUBLIC"."TABLE2" ("C1", "C2") VALUES ('u', 'v');

DROP TABLE "PUBLIC"."TABLE3" IF EXISTS;

CREATE TABLE "PUBLIC"."TABLE3" (
  "C1" VARCHAR(10),
  "C2" VARCHAR(10)
);

INSERT INTO "PUBLIC"."TABLE3" (
  "C1",
  "C2"
)
SELECT
(SELECT
"T2"."C1" AS "T2C1"
FROM "PUBLIC"."TABLE2" "T2"
WHERE "T2"."C1" = "T1"."C1") AS "T2C1",
"T1"."C2" AS "T1C2"
FROM "PUBLIC"."TABLE1" "T1"
ORDER BY "T2C1", "T1C2";

* *DSV_COL_DELIM=,
* *DSV_ROW_DELIM=\n
* *DSV_TARGET_FILE=results.csv

\x "PUBLIC"."TABLE3"

如何更改我的脚本,使“[null]”替换为“?”在生成的CSV文件中?

2 个答案:

答案 0 :(得分:1)

取决于dbengine:

for hSQL ... 3个作品中的任何一个根据:hSQLDB.org

合并(MOST)

NVL(oracle)

IFNULL(MySQL的) 编辑添加(...(哎呀我忘了!)

SELECT
coalesce((SELECT
"T2"."C1" AS "T2C1"
FROM "PUBLIC"."TABLE2" "T2"
WHERE "T2"."C1" = "T1"."C1"),'?') AS "T2C1", 
"T1"."C2" AS "T1C2"
FROM "PUBLIC"."TABLE1" "T1"
ORDER BY "T2C1", "T1C2";

答案 1 :(得分:0)

不是从表中插入值,而是在插入上使用case语法来检查值是否为空。

这样的事情:

case when value = [null] then ? else variable end