In terms of x compared to y.
- Does x comply with sql standards better? [apologies if subjective]
- Is x more efficient than y?
- Or are these scripts completely different and to be used in different contexts?
x
SELECT *
FROM a
INNER JOIN b ON
COALESCE(b.columntojoin, b.alternatecolumn) = a.columntojoin
y
SELECT *
FROM a
INNER JOIN b ON
(case when b.columntojoin is null then b.alternatecolumn else b.columntojoin end) = a.columntojoin
COALESCE
is essentially a shorthanded CASE
statement.
Both are exactly the same.
There is also ISNULL
in SQL Server (differs in other DBMSs), but that's actually a non-standard feature and is actually more limited that COALESCE
.
In this case I would use COALESCE
( which provides more levels than ISNULL
) rather than the CASE
stement.
The CASE
statement seems a bit bulky here, seeing as you are only checking for NULL
s anyway.
来源:https://stackoverflow.com/questions/15523000/coalesce-or-case-more-efficient-and-or-standard