I have a sample query below:
Select column1, column2, column3
from table1
where column1 = 3
and the result is blank
Question: How to update the query that it will return this default result instead of blank rows?
| column1 | column2 | column3 |
| ------------ | ------------ | ------------ |
| no rows found | | |
I tried a case expression but its still returning a blank row
2条答案
按热度按时间uqjltbpv1#
You have to return a record when your search does not match anything
You can achieve this result using
UNION
operator, to add to your result set the default row you desire when main search give no results:you can write it using cte for readability
yduiuuwa2#
The existing answers require reading from the table multiple times. It is possible to do this with only reading once from the table
Note that there should be no actual sorting involved, as the compiler can see that different unique values come out of each part of the union.