
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to update User Logged in Time for a specific user in MySQL?
For this, use ORDER BY along with LIMIT. Let us first create a table wherein we have a column with User id, logged in time, and name −
mysql> create table DemoTable1911 ( UserId int, UserLoggedInTime time, UserName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1911 values(100,'7:32:00','Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1911 values(101,'5:00:00','David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1911 values(102,'6:10:20','Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1911 values(103,'3:55:00','Carol'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1911;
This will produce the following output −
+--------+------------------+----------+ | UserId | UserLoggedInTime | UserName | +--------+------------------+----------+ | 100 | 07:32:00 | Chris | | 101 | 05:00:00 | David | | 102 | 06:10:20 | Mike | | 103 | 03:55:00 | Carol | +--------+------------------+----------+ 4 rows in set (0.00 sec)
Here is the query to update logged in time for a specific user “Carol” −
mysql> update DemoTable1911 set UserLoggedInTime='12:30:45' where UserName='Carol' order by UserId DESC Limit 1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1911;
This will produce the following output −
+--------+------------------+----------+ | UserId | UserLoggedInTime | UserName | +--------+------------------+----------+ | 100 | 07:32:00 | Chris | | 101 | 05:00:00 | David | | 102 | 06:10:20 | Mike | | 103 | 12:30:45 | Carol | +--------+------------------+----------+ 4 rows in set (0.00 sec)
- Related Articles
- Check privileges (grants) for a specific user in MySQL?
- How to prevent a user from accessing a specific schema in MySQL?
- Display all grants of a specific user in MySQL
- How to get the currently logged-in user account with Azure CLI in PowerShell?
- How to display grant defined for a MySQL user?
- Display all grants for user in MySQL
- How can we change MySQL user password by using UPDATE statement?
- How to Add Cron Jobs to A Specific User in a Linux System
- MySQL query to group data in the form of user login time per hour and get the records of the users logged in the recent hour?
- MySQL appears to DROP USER but user still exists in MySQL.users table?
- How to log in as a different user on MySQL?
- In MySQL, how can we display time in other format specified by the user?
- How Can we permanently define user-defined variable for a client in MySQL?
- How to use user variables in MySQL LIKE clause?
- How can we grant privileges to a MySQL user?

Advertisements