简体   繁体   English

从表中选择*其中id =在codeigniter中选择最大id

[英]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 ? 如何获取id =表中最后一个id的数据并显示所有值而不是一个值?

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? 我有表名称= tb_invoice和3列(ID,月,年)如何在Codeigniter中获取最后一个ID并显示结果(ID,月,年)? 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)? 结果只是显示id,如何显示所有值(id,月,年)?

尝试这个

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

Try using insert_id. 尝试使用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 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: 感谢@Sunny Khatri提供的代码,此方法有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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