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

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

为此,使用ORDER BY与LIMIT。让我们首先创建一张表,其中包括用户id、登录时间和用户名这3列:

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)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程