如何为 MySQL 中特定用户更新用户登录时间?
为此,请将 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)
广告