简体   繁体   English

SELECT CASE/JOIN 无行时

[英]SELECT CASE/JOIN when no rows

Sorry this one is a bit of a headache.抱歉,这个有点头疼。 I'll start with the example:我将从示例开始:

Tables:表:

TownCountry城镇国家

Record |  Town  | CountryCode
-------+--------+-------------
1      | London | A1
2      | Cardiff| A2
3      | Hull   | A1
4      | Luton  | A1

ReFData参考数据

Type    |  Code   | Country
--------+---------+-------------
Country | A1      | England
Country | A2      | Wales

If my query is:如果我的查询是:

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

I get:我得到:

London | Wales

However, if I change the code for Wales to A3, and keep the query the same, by result returns no rows.但是,如果我将威尔士的代码更改为 A3,并保持查询相同,则结果不会返回任何行。

What I want, in the example where Wales is A3, is for my result to be:在威尔士为 A3 的示例中,我想要的结果是:

London | (empty)

I've tried COALESCE:我试过 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

but this returned no rows但这没有返回任何行

I also tried select case, right and left joins, but still no rows.我也试过 select 案例,左右连接,但仍然没有行。

Here's a simpler example that my good friend just gave me while discussing:这是我的好朋友在讨论时给我的一个更简单的例子:

Towns城市

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

I want to return我想回来

a.Town | b.Town | c.Town | d.Town
-------+--------+--------+--------
London | Cardiff| NULL   | Luton

Any help much appreciated.非常感谢任何帮助。

If you want to keep rows even where there is no match on the column you're joining on, you need to do an OUTER JOIN rather than an INNER JOIN .如果您想保留行,即使您要加入的列上没有匹配项,您需要执行OUTER JOIN而不是INNER JOIN

http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp?topic=/com.ibm.db2.luw.apdv.porting.doc/doc/r0052878.html http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp?topic=/com.ibm.db2.luw.apdv.porting.doc/doc/r0052878.html

You're not really doing joins, and you need an outer join (ie LEFT JOIN ).你并不是真的在做连接,你需要一个外连接(即LEFT JOIN )。

What you want is something like this:你想要的是这样的:

select a.Town, b.Country
from TownCountry a
left join RefData b on b.Code = a.CountryCode
left join TownCountry c on c.CountryCode = b.Code and c.Record=2
where a.Record=1;

EDITED : I put "and c.Record=2" into the join clause.编辑:我将“and c.Record=2”放入连接子句中。 This little trick is a good one - it retains the condition, but doesn't require a joined row这个小技巧很好——它保留了条件,但不需要连接行

The problem here is that the Translation table does not have entry for blank raw values.这里的问题是 Translation 表没有空白原始值的条目。 As a result, there is nothing in the Translation table that matches so no rows are returned.因此,Translation 表中没有任何匹配项,因此不会返回任何行。

This particular problem can be solved by adding a row to the Translation table, or more precisely, using union to add the row:这个特殊问题可以通过向 Translation 表中添加一行来解决,或者更准确地说,使用联合来添加行:

select a.Town, b.Country from TownCountry a, 
(select Code, Country from RefData b
union select '' as Code, 'Not found' as Country from RefData c), TownCountry c
where a.Record=1
and b.Code=c.CountryCode and c.Record=2

SQL Love, Wing SQL 爱,翼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM