抱歉,这个有点头疼。我将从示例开始:
表:
城镇国家
Record | Town | CountryCode
-------+--------+-------------
1 | London | A1
2 | Cardiff| A2
3 | Hull | A1
4 | Luton | A1
参考数据
Type | Code | Country
--------+---------+-------------
Country | A1 | England
Country | A2 | Wales
如果我的查询是:
select a.Town, b.Country from TownCountry a, RefData b, TownCountry c
where a.Record=1
and b.Code=c.CountryCode and c.Record=2
我得到:
London | Wales
但是,如果我将威尔士的代码更改为 A3,并保持查询相同,则结果不会返回任何行。
在威尔士为 A3 的示例中,我想要的结果是:
London | (empty)
我试过 COALESCE:
select a.Town, COALESCE(b.Country,'empty') from TownCountry a, RefData b, TownCountry c
where a.Record=1
and b.Code=c.CountryCode and c.Record=2
但这没有返回任何行
我还尝试了选择案例、左右连接,但仍然没有行。
这是我的好朋友在讨论时给我的一个更简单的例子:
城市
Record | Town
-------+--------
1 | London
2 | Cardiff
4 | Luton
select a.Town, b.Town, c.town, d.Town
from Towns a, Towns b, Towns c, Towns d
where a.Reocrd=1 and b.Reocrd=2 and c.Reocrd=3 and a.Reocrd=4
我想回来
a.Town | b.Town | c.Town | d.Town
-------+--------+--------+--------
London | Cardiff| NULL | Luton
非常感谢任何帮助。