简体   繁体   中英

select * from table where id = select max id in codeigniter

How to get data where id = last id in table and show all value not one value ?

I have table name=tb_invoice and 3 column (id, month, year) How to get last id and show result (id, month, year) in codeigniter? here my code :

    $this->db->select_max('id');
    $query  = $this->db->get('tb_invoice');
    return  = $query->row_array();

the result just show id, how to show all value (id, month, year)?

尝试这个

$this->db->select('id, month, year')->order_by('id','desc')->limit(1)->get('tb_invoice')->row('id');

Try using insert_id.

$this->db->insert_id();
$query  = $this->db->get('tb_invoice');
return  = $query->row_array();

Or you can use

$this->db->select_max('{primary key}');
$query  = $this->db->get('tb_invoice');
return  = $query->row_array();

Please Try this:

$maxid = $this->db->query('SELECT MAX(id) AS `maxid` FROM `tb_invoice`')->row()->maxid;

UPDATE

This will work even if your table is empty (unlike my example above):

$maxid = 0;
$row = $this->db->query('SELECT MAX(id) AS `maxid` FROM `tb_invoice`')->row();
if ($row) {
    $maxid = $row->maxid; 
}

thanks to @Sunny Khatri for code, this is worked:

$this->db->limit(1);
$this->db->order_by('id', 'DESC');
$query  = $this->db->get('tb_invoice');
return $query->row_array();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM