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. जावा तैयार कथन निष्पादित नहीं हो रहा है

  2. माइक्रोटाइम के लिए दशमलव लंबाई (सच)?

  3. JSON स्ट्रिंग के साथ फ़ील्ड खोजने के लिए MySQL क्वेरी

  4. अन्य पंक्तियों के आधार पर परिणाम सेट कैसे लौटाएं

  5. त्रुटि:mysqladmin:रीफ्रेश विफल; त्रुटि:'अज्ञात त्रुटि'