0

我用它来连接两个表(这里省略了一些字段)。原始表中的所有字段都定义为 VARCHAR,NOT NULL,默认为 ''。

  CREATE TABLE newtable
  SELECT
     table1.field1,
     table1.field2,
     ...
     table2.field1,
     table2.field2,
     ...
 FROM
    table1
 LEFT JOIN table2 ON table1.id = table2.id

连接本身正在工作,但是,表 1 有一些不在表 2 中的行。当我需要它们为空字符串时,表 2 中的字段被添加为这些条目的 NULL。创建表时有没有办法做到这一点?

4

2 回答 2

1
IFNULL(table1.field1,'') AS table1_field1,

IFNULL如果它不为空,则保留该值,否则将其替换为第二个参数,在您的情况下为空字符串

于 2012-11-28T22:24:18.567 回答
1

如果您希望 table2 中的列为 '' ..put an nvl ...这在 oracle 中有效

CREATE TABLE newtable as 
  SELECT
     table1.field1,
     table1.field2,
     ...
     nvl(table2.field1,''),
     nvl(table2.field2,''),
     ...
 FROM
    table1
 LEFT JOIN table2 ON table1.id = table2.id
于 2012-11-29T11:42:32.143 回答