Mysql
 sql >> Baza danych >  >> RDS >> Mysql

Jak uzyskać nowy balans biegowy z istniejącego balansu?

Case statement poradzi sobie z twoim stanem.

SELECT id ID, token TK, actual_pay PAY,
       IF(@rtp IS NULL, @rtp:=token, @rtp:example@sqldat.com+actual_pay) RTP,
       IF(@bal IS NULL, @bal:=actual_pay-token, @bal:example@sqldat.com) BAL,

       (case IF(@rtp IS NULL, @rtp:=token, @rtp:example@sqldat.com+actual_pay)
         when IF(@rtp IS NULL, @rtp:=token, @rtp:example@sqldat.com+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:example@sqldat.com)
        when IF(@bal IS NULL, @bal:=actual_pay-token, @bal:example@sqldat.com) <= 
             (select token from token_table where id = a.id+1)
        then 0
       else 
        IF(@bal IS NULL, @bal:=actual_pay-token, @bal:example@sqldat.com) - 
        (select token from token_table where id = a.id+1)
      end case) nbal,

      (case IF(@bal IS NULL, @bal:=actual_pay-token, @bal:example@sqldat.com)
        when IF(@bal IS NULL, @bal:=actual_pay-token, @bal:example@sqldat.com) <= 
             (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:example@sqldat.com)
       else 
        IF(@bal IS NULL, @bal:=actual_pay-token, @bal:example@sqldat.com) - 
        (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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Wyjaśnienie Wordnet SQL

  2. Zapomniane hasło root do mysql

  3. Błąd Rails-MySQL - niezainicjowana stała MysqlCompat::MysqlRes

  4. Codeigniter pokazuje pustą stronę bez błędu

  5. Konwertuj 24-godzinny sformatowany czas na liczbę dziesiętną