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

mysqlmysqli database更新于 2024/1/9 1:34:00

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

mysql> create table DemoTable1911
   (
   UserId int,
   UserLoggedInTime time,
   UserName varchar(20)
   );
Query OK, 0 rows affected (0.00 sec)

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

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)

使用 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;
Query OK, 1 row affected (0.00 sec)
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)

相关文章