Mysql
 sql >> база данни >  >> RDS >> Mysql

Как да получите новия текущ баланс от съществуващия баланс?

Case statement може да се справи с вашето състояние.

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. PHP обвързване на заместващ знак

  2. Използване на PHP SESSION променливи за съхраняване на резултатите от MySQL заявки

  3. Печат на ранг от сума в PHP и MySQL в 1,1,3,4 и т.н. Ред

  4. Каква е постоянството на препратките и присъединяванията в MySQL?

  5. Вложени класове - CustomRowMapper !! Вече не е проблем!! - Част 1