sql >> Databasteknik >  >> RDS >> Mysql

Hur får man det nya löpande saldot från befintligt saldo?

En Case statement kan hantera ditt tillstånd.

SELECT id ID, token TK, actual_pay PAY,
       IF(@rtp IS NULL, @rtp:=token, @rtp:[email protected]+actual_pay) RTP,
       IF(@bal IS NULL, @bal:=actual_pay-token, @bal:[email protected]) BAL,

       (case IF(@rtp IS NULL, @rtp:=token, @rtp:[email protected]+actual_pay)
         when IF(@rtp IS NULL, @rtp:=token, @rtp:[email protected]+actual_pay) <= 
              (select token from token_table where id = a.id+1)
         then 1
        else 0
       end case) stat,

      (case IF(@bal IS NULL, @bal:=actual_pay-token, @bal:[email protected])
        when IF(@bal IS NULL, @bal:=actual_pay-token, @bal:[email protected]) <= 
             (select token from token_table where id = a.id+1)
        then 0
       else 
        IF(@bal IS NULL, @bal:=actual_pay-token, @bal:[email protected]) - 
        (select token from token_table where id = a.id+1)
      end case) nbal,

      (case IF(@bal IS NULL, @bal:=actual_pay-token, @bal:[email protected])
        when IF(@bal IS NULL, @bal:=actual_pay-token, @bal:[email protected]) <= 
             (select token from token_table where id = a.id+1)           
        then
            (select token from token_table where id = a.id+1)  -
            IF(@bal IS NULL, @bal:=actual_pay-token, @bal:[email protected])
       else 
        IF(@bal IS NULL, @bal:=actual_pay-token, @bal:[email protected]) - 
        (select token from token_table where id = a.id+1)
      end case) ntk

FROM token_table a
JOIN (SELECT @rtp:=NULL, @bal:=NULL) b;


  1. Automatisera tabellmodellbearbetningen av Analysis Services-databaser (SSAS) i SQL Server

  2. Orsakas av:java.sql.SQLException:Teckenkodningen 'utf8mb4' stöds inte

  3. Finns det ett "anslut med"-alternativ i MySQL?

  4. Hur kontrollerar man om en rad finns i MySQL? (dvs kontrollera om ett e-postmeddelande finns i MySQL)