如何在MySQL中为特定用户更新用户登录时间?

为此,将ORDER BY与LIMIT一起使用。让我们首先创建一个表,其中有一个包含用户ID,登录时间和名称的列-

mysql> create table DemoTable1911
   (
   UserId int,
   UserLoggedInTime time,
   UserName varchar(20)
   );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable1911 values(100,'7:32:00','Chris');
mysql> insert into DemoTable1911 values(101,'5:00:00','David');
mysql> insert into DemoTable1911 values(102,'6:10:20','Mike');
mysql> insert into DemoTable1911 values(103,'3:55:00','Carol');

使用select语句显示表中的所有记录-

mysql> select * from DemoTable1911;

这将产生以下输出-

+--------+------------------+----------+
| 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)

这是用于更新特定用户“ Carol”的登录时间的查询-

mysql> update DemoTable1911
     set UserLoggedInTime='12:30:45'
     where UserName='Carol'
     order by UserId DESC
     Limit 1;
Rows matched: 1  Changed: 1 Warnings: 0

让我们再次检查表记录-

mysql> select * from DemoTable1911;

这将产生以下输出-

+--------+------------------+----------+
| 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)