Home > database >  According to the different SQL query results
According to the different SQL query results

Time:09-15

If there id=1, table 1: select * from table 1, otherwise do: select * from table 2
Oracle to how to write?
Table 1 and table 2 structure, just is not the same as the content

CodePudding user response:

- can be written as such, but I suggest you write better in two steps,

Select * from t1 where id=1
Union all
Select * from t2 where not the exists (select * from t1 where id=1)

CodePudding user response:

Declare
V_cnt int.
The begin
Select count (1) into v_cnt from table 1 where id=1 and rownum<2;
If v_cnt & gt; 0 then
Select * from table 1
The else
Select * from table 2
end if;
The end;
So, probably with hand written, no specific grammar, proofreading

CodePudding user response:

SELECT
CASE
WHEN the rowcount & gt; 0 THEN
Select *
From table 1
The ELSE
SELECT *
FROM table 2
END
The FROM (
SELECT count (*) the rowcount
FROM table 1
WHERE id=1) a

CodePudding user response:

3 f, is realized by using the stored procedure support

CodePudding user response:

Select * from tab1 where the exists (select * from tab1 t1 where t1. Id=1)
Union all
Select * from tab2 where not the exists (select * from tab1 t2 where t2. Id=1)

CodePudding user response:

Select * from a1 where the exists (select null from dual where a1. Id=1)
Union all
Select * from a2 where not the exists (select null from a1 where a1. Id=1)
  • Related