phpsqlcodeigniterlimitquery-builder

How to implement "SELECT TOP 1" for CodeIgniter?


I need to get only 1 record from a SQL query result.

We use "SELECT TOP 1" in standard SQL, but how can we do that in CodeIgniter? Is there any function for that?


Solution

  • with LIMIT

    $this->db->limit(1);
    $query = $this->db->get('my_table');
    $myRow = $query->row();
    

    with OFFSET and LIMIT

    $query = $this->db->get('mytable', 0, 1);
    $myRow = $query->row();