0

I am not familiar in this style of coding syntax but I want to use this for my project. I have a question on how to integrate this query:

SELECT max(id) FROM assignment_billing WHERE assignment_id = 37

into this syntax.

public function fetchBillingByParentId($db,$id) {
    $select = $db->select()->from('assignment_billing')->where('assignment_id = '.$id);
    $stmt = $select->query();
    $result = $stmt->fetchAll();
    return $result;
}

I want to use max to get the highest id on that table but my syntax doesn't work.

public function fetchBillingByParentId($db,$id) {
    $select = $db->select('max(id)')->from('assignment_billing')->where('assignment_id = '.$id);
    $stmt = $select->query();
    $result = $stmt->fetchAll();
    return $result;
}

Did I forgot something? the first syntax is working though. But in the second syntax it doesn't return any value? I think I have an error in 'select('max(id)')->' line. What might be the proper arrangement in this kind of syntax?

1
  • 2
    What framework are you using that has that database API? Try reading its documentation to see how you do it. Commented Aug 30, 2013 at 0:08

1 Answer 1

1

Anytime you use an aggregate you must alias the column.

$select = $db->select('max(id) AS balloon')->from('assignment_billing')->where('assignment_id = '.$id);

You would then reference balloon as the column.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.