Jarich
Jarich

Reputation: 337

How to get largest value using 'MAX' on PHP mysql

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?

Upvotes: 0

Views: 82

Answers (1)

Kermit
Kermit

Reputation: 34055

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.

Upvotes: 1

Related Questions