看来这个查询并不容易理解。所以我试着让它变得简单。以下是我的创建表查询并插入查询......
create table test(
eancode varchar(50)
);
create table tab_fr(
eancode varchar(50),
product varchar(50)
);
create table tab_it(
eancode varchar(50),
product varchar(50)
);
create table tab_at(
eancode varchar(50),
product varchar(50)
);
insert into test (eancode) values ('1');
insert into test (eancode) values ('2');
insert into test (eancode) values ('3');
insert into test (eancode) values ('4');
insert into tab_fr (eancode,product) values ('1','prod1');
insert into tab_fr (eancode,product) values ('2','prod2');
insert into tab_fr (eancode,product) values ('3','prod3');
insert into tab_fr (eancode,product) values ('4','prod4');
insert into tab_it (eancode,product) values ('1','prod1');
insert into tab_it (eancode,product) values ('3','prod3');
insert into tab_at (eancode,product) values ('2','prod2');
insert into tab_at (eancode,product) values ('4','prod4');
现在我想以下列格式检索数据:
test.eancode | tab_fr.product | tab_it.product | tab_at.product |
1 | prod1 | prod1 | NULL
2 | prod2 | NULL | Prod2的
3 | prod3 | prod3 | NULL
4 | prod4 | NULL | prod4
我希望可以轻松理解这种新格式和样本数据。期待您的帮助。
答案 0 :(得分:0)
编辑原始答案,以便您阅读:
您需要的是使用OUTER JOIN
SELECT test.eancode, tab_fr.product, tab_it.product, tab_at.product
FROM test
LEFT JOIN tab_fr ON test.eancode = tab_fr.eancode
LEFT JOIN tab_it ON test.eancode = tab_it.eancode
LEFT JOIN tab_at ON test.eancode = tab_at.eancode;
对OUTER JOINS进行一些调查,考虑缺失值。
乔恩