How to display an empty record when the oracle sql query result is empty

I often write simple or complex SQL queries in my work. Sometimes I need to do some special processing based on the SQL query to achieve special display effects. This experience will introduce the oracle sql query, if the query result is empty (the query result does not have a record), how to display an empty record (there is a record, but the fields in the record are empty), and when the query result is not When it is empty, it will be displayed according to the normal query results. Here we use the oracle rownum attribute.

Method one

First, let's take a look at the display situation when the query result is empty when the ordinary query of Oracle SQL is shown in the figure below. It can be seen that there is no record in the query result without special processing, and the query sql here is marked as A query .

How to display an empty record when the oracle sql query result is empty




②, Then, the first time we thought that since the query result is empty, an empty record is displayed, we first have to create an empty record, so we thought of using an empty record to union with the above SQL query, and got the following search result:

How to display an empty record when the oracle sql query result is empty





③ From the above query results, we seem to see that it is the expected effect we want to achieve, but the problem comes, once my query condition changes (the loginname parameter value in the query condition changes), will the result be satisfactory? ? Let's take a look:

How to display an empty record when the oracle sql query result is empty





Method two

①、

From the query results in the previous step, we can see that when the original query sql query result is not empty, we simply follow the method of union an empty record, because we want to dynamically process the query result, that is, when the query result is not When it is empty, it will be displayed according to the normal query result. Only when the query result is empty, an empty record will be displayed. Therefore, here we need to introduce the oracle rownum pseudo column for special processing. First look at the simple query results using rownum, the query sql here is denoted as B query :

How to display an empty record when the oracle sql query result is empty
How to display an empty record when the oracle sql query result is empty










②、

From the query results in the previous step, we can see that rownum is actually the number of the SQL query result record. We can achieve a special query effect by controlling the range of rownum. For example, rownum <= n can query the first n records in the query result. What we need to determine is this n value. Here we use the decode function to determine:

How to display an empty record when the oracle sql query result is empty
How to display an empty record when the oracle sql query result is empty















③、

Finally, we integrate to get the final query sql, that is, when the A query result is empty, we take n = 1, when the A query record is not empty, we take n = A total number of query records, and finally rownum <= The query condition of n is written to the B query to get the desired result, as follows:

How to display an empty record when the oracle sql query result is empty
How to display an empty record when the oracle sql query result is empty


















This article is reproduced from: https://jingyan.baidu.com/article/73c3ce28178ae8e50343d91e.html

Published 28 original articles · Like 15 · Visits 110,000+

Guess you like

Origin blog.csdn.net/z3h0a5n8g8x9i9a2o3/article/details/79662949