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.
What I want, in the example where Wales is A3, is for my result to be:
London | (empty)
I've tried 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.
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
.
You're not really doing joins, and you need an outer join (ie 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. 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. As a result, there is nothing in the Translation table that matches so no rows are returned.
This particular problem can be solved by adding a row to the Translation table, or more precisely, using union to add the row:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.