我需要从不属于当前模型的表中获取最后插入的ID,也不需要当前数据库连接:现在我在表中(pdtlisting )但我想从表中获取最后插入的id(deallisting)。我怎么能得到它?
答案 0 :(得分:1)
试试这个..
它用于mySql ..
LAST_INSERT_ID()
或强>
它用于Php ..
mysql_insert_id()
All the last_insert_id functions (be they PHP wrappers or the native mySQL one) typically refer to the last ID created using the current database connection. The last login was probably not created during the same request you are showing the table in, so this method won't work for you.
Use a normal SELECT to find out the newest login instead - e.g. by using ORDER by creationtime DESC LIMIT 1.
或强> 在Yii中,您可以找到最后一个插入的ID ..
Yii::app()->db->getLastInsertID();
你也可以参考这个链接..
http://www.yiiframework.com/doc/api/1.0/CActiveRecord#primaryKey-detail