2

我有 2 张桌子:


表 1(含英文内容):

id (primary key, auto increment)
title
text
...

表 2(葡萄牙语内容):

id (foreing Key to table 1 ID)
title
text
…

内容示例:

表 1(含英文内容):

id: 4
title: Hello World!
text: hello world, i need you!
… : anothers fields

表 2(葡萄牙语内容):

id: 4
title: Ola mundo
text: NULL
…: another fields with NULL values OR portuguese contents

完美的结果:

结果:

id: 4
title: Ola mundo
text: hello world, i need you!
…: another fields with english content with NULL values in portuguese table OR portuguese content if is not null values

这个问题和结果是否相同:stackoverflow,但我不知道哪些字段是 NULL 以及表中有多少字段。我知道替代表(葡萄牙语)具有相同的列名。我需要 COALESCE(table1.*,table2.*) 之类的东西,但这不起作用。

 SELECT COALESCE(table1.*, table2.*) FROM table1 LEFT JOIN table2 USING(id) WHERE id = 4;

但在表 2 中,可能没有记录 id “4”……</p>

我怎样才能得到这个结果?

谢谢

4

1 回答 1

-1

列名应单独合并

SELECT  a.id,COALESCE(b.title, a.title) Title,COALESCE(b.text, a.text) Text
FROM    Table1 a LEFT JOIN Table2 b
ON a.id = b.id
于 2013-07-07T17:11:16.667 回答