j4mie/idiorm

how to do arithmetic operator in idiorm ?

Closed this issue · 3 comments

how to i write this in idiORM i already read the documentation but still dont understand,can u guys please help me,sorry for my bad english

i want to wrote this query in idiORM

SELECT cust_name,opening_amt, payment_amt, outstanding_amt
FROM customer 
WHERE(outstanding_amt - payment_amt) as receiveAmt;

thanks in advance.

There is so arithmetic operation in it? Just a simple select.

tag commented

Chances are good that the query you've posted is invalid, and even if it isn't, it's possible it's not what you intend.

Perhaps this is what you intend?:

SELECT
  cust_name,opening_amt, payment_amt, outstanding_amt, 
  (outstanding_amt - payment_amt) AS receive_amt
FROM customer
;

MySQL doesn't permit aliasing in the where clause. It's not clear which database provider you're using, but this is not unusual.

Putting the subtraction in the WHERE clause will filter out rows where receiveAmt is zero. (Perhaps this is what you intend). If so, cleaner (more explicit) would be:

SELECT
  cust_name,opening_amt, payment_amt, outstanding_amt, 
  (outstanding_amt - payment_amt) AS receive_amt
FROM customer
WHERE
  (outstanding_amt - payment_amt) = 0
;

In IdiORM, use the where_raw() method.

$payments = ORM::for_table('customer')
            ->where_raw('(outstanding_amt - payment_amt) = 0')
            ->find_many();

Lastly, questions like this are better asked on StackOverflow. Not only are there more eyes there, it's a support venue, whereas GitHub issues is better used for bugs/enhancement requests.

got it !!,thanks everyonee