Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Làm thế nào để có được Số dư chạy mới từ Số dư hiện có?

Một Case statement có thể xử lý tình trạng của bạn.

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. thực thi php thông qua mysql?

  2. Codeigniter Active Record HAVING / WHERE db.field =db.field

  3. Lập phiên bản bảng (bảng lịch sử) và mối quan hệ bảng Thứ tự

  4. PHP MYSQL - Chèn vào mà không sử dụng tên cột nhưng với trường autoincrement

  5. Câu hỏi về MySQL - Khóa duy nhất Không hoạt động chính xác, hay tôi đang hiểu nhầm?