Re: Need help on MySQL query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Dec 11, 2008 at 4:52 PM, Rahat Bashir <rahat.bashir@xxxxxxxxx>wrote:

> Hi Experts,
>
> EID Mubarak to all.
>
> I need your help on writing a MySQL query.
>
> Scenario:
>
> CREATE TABLE transaction
> (
> `id` int NOT NULL ATUTO INCREMENT,
> `date` datetime  NOT NULL,
> `withdrawn` double (12,2) NULL,
> `deposit` double (12,2) NULL
> );
>
> SELECT * FROM transaction;


SELECT *,  deposit-withdrawn AS balance FROM transaction

Although i would suggest a transaction table to only have positive and
negative balances. Then you can do all sorts of things with it like get the
sum of all transactions a lot easier etc.

e.g.

SELECT SUM(balance) AS user_balance FROM transaction WHERE user_id=1

if you have a user id e.g. Are you working for a bank? :-)

>
>
> id              date
> withdrawn                  deposit
> -----            -----------------------------
> ---------------                 --------------
> 1               2008-12-01 00:00:00
> NULL                        10000.00
> 2               2008-12-02 00:00:00                 4000.00
>                    NULL
> 3               2008-12-04 00:00:00                 2000.00
>                    NULL
> 4               2008-12-05 00:00:00
> NULL                        4500.00
> 5               2008-12-06 00:00:00                 500.00
>                     1500.00
>
> The above is all I have. I want to make query which should output an extra
> calculated column named "balance", something like following:
>
> Expected output from query:
> id              date
> withdrawn                  deposit                balance
> -----            -----------------------------
> ---------------                 --------------             -------------
> 1               2008-12-01 00:00:00
> NULL                        10000.00             10000.00
> 2               2008-12-02 00:00:00                 4000.00
>                    NULL                  6000.00
> 3               2008-12-04 00:00:00                 2000.00
>                    NULL                  4000.00
> 4               2008-12-05 00:00:00
> NULL                        4500.00               8500.00
> 5               2008-12-06 00:00:00                 500.00
>                     1500.00               9500.00
>
> Thanks in advance
>
>
> --
> Rahat Bashir
> Dhaka, Bangladesh
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux