When oracle query changes the status field based on the time of the query field

        A recently encountered the following scenario in the development process: oracle product table inside the database product offline status field and a time field shelf_time goods_statu, there is a scanning timing of the timing tasks product table, if the current state is a product (on-line state), and offline time is less than current time product changed to 0 (offline state), but the timing of the task can not guarantee real-time, so to add query logic to determine this, the following is a solution

    select decode(

      (CASE WHEN
        (shelf_time < sysdate AND goods_status = '1' ) THEN -1
      END ),-1,0,goods_status) as goods_status

      from T_GOODS 

Decode and use the oracle CASE function perfect combination to solve the problem.

Usage of decode functions substantially as follows:

    decode (condition value 1, returns the value 1, value 2, returns the value 2, ... value n, the return value of n, the default value)

   The meaning of the function is as follows:
   the IF condition THEN = value. 1
    the RETURN (translation value. 1)
   ELSIF condition THEN = value 2
    the RETURN (translated value 2)
    ......
   ELSIF condition THEN = value n-
    the RETURN (translated value n-)
   the ELSE
    the RETURN (default)
   the END the IF

 Basic usage of CASE function is as follows:

  case shelf_time 

  when shelf_time < sysdate  THEN  -1

  when shelf_time = sysdate  THEN  0

  else 1 end

 

  

Guess you like

Origin www.cnblogs.com/wllbdml/p/10978965.html