BarryWalsh
BarryWalsh

Reputation: 1340

Codeigniter select_max and return other row fields

I'm trying to select the max bid amount on a particular item and return more information from the row that the max bid is found in.

At the moment I have

    $item_id = $this->input->post('item_id');
    $this->db->from('bids');
    $this->db->where('item_id', $item_id);
    $this->db->where('outbid_alert', '1');
    $this->db->select_max('bid_amount');
    $query = $this->db->get();
    return $query->result();    

This returns the max bid for the item and that's as far as I have gotten. What's the best way to get the rest of the fields from that row? Run another query or use a subquery?

Thanks!

Upvotes: 0

Views: 889

Answers (1)

gen_Eric
gen_Eric

Reputation: 227230

If you want to return the fields from the row with the highest bid_amount, just ORDER BY bid_amount and select only the first row.

$item_id = $this->input->post('item_id');

$this->db->from('bids');
$this->db->where('item_id', $item_id);
$this->db->where('outbid_alert', '1');

$this->db->select('*');

$this->db->order_by('bid_amount', 'DESC');
$this->db->limit(1);

$query = $this->db->get();
return $query->result();

Upvotes: 1

Related Questions