sql - If no result then another query, can be combined to one query? -
phone_book +----+---------+-----------+--------------+ | id | key | code | value | +----+---------+-----------+--------------+ | 1 | max_val | 111 | reset | +----+------+--------------+--------------+ | 2 | min_val | 222 | set | +----+------+--------------+--------------+ | 3 | min_val | 0 | na | +----+---------+-----------+--------------+
key , code combination primary key.
requirement:
if key , code present, return value.
if key present , code not exist return value of code 0.
implementation:
achieved using multiple query. syntax used jpql
1) "select param phone_book param upper(key)=:paramkey , code=:estcode";
if returns null, while shoot query
2) "select param phone_book param upper(key)=:paramkey , code=:o";
what looking :
can achieve in 1 query, or better way ?
thanks in advance.
in oracle sql, below suffice need. no need write plsql this.
select key, nvl(code,0) -- make sure if code null value 0 phone_book (key not null , code not null) -- in fetching value when key , code present or ( key not null , code null); -- in fetching value when key present , code null.
Comments
Post a Comment