Getting Last Inserted Id From A Table Not Part Of The Current Model

hi friends

I need to get the last inserted ID from a table which is not part of the current model . right now I am in table (User) but I want to get the last inserted id from table (check_login). How can i get it??

Please describe why you want to do so. Remember that if ‘something happens’ during the execution of a function that relies on the last inserted id (e.g. another insert), your result might get a little strange. However, if you want to, please have a look at using ‘MAX’ in MySQL, which is described in this topic.

hi Emile Bons

i want to store the logout time in Check_login table against the current logedin user. for that 1st i need to get the last inserted id of Check_login table then against the id update the logout_time column.

Then you’re fine with the solution posted; create a query with that one that selects the max(id) or max(login_time) from check_login table with the user_id in the where condition.

thanks Emile Bons.

i solved it .

here is the code.





$lastId = Yii::app()->db->createCommand('SELECT id FROM login_check where user_id=user_id ORDER BY id DESC LIMIT 1')->queryScalar();