首页 > 解决方案 > getting the 1st row in a database

问题描述

I want to get the 1st row of the result depends on which build the room is. For example Building 1 have 1-200 rooms and Building 2 have 201-400 rooms. The code I tried is below. I have used the MIN in the where clause but I got all the rooms instead of having one.

$query = $this->db->query("SELECT * FROM `ha_utility_reading`");
if ($query->num_rows == 0) {

    echo "some data match";
    $lastroom = $this->db->select("*")->from("rooms")
      ->where("(SELECT MIN(room_num) FROM ha_rooms) and bldg_num = '$bldg_num'")
      ->get()->result_array();

    foreach($lastroom as $key => $test) {
        $output['room_num'][] = $test['room_num'];
        json_encode($output);
    }

标签: mysqlicodeigniter-3

解决方案


尝试这个,

select * from rooms order by room_num asc limit 1;


推荐阅读