CONNECTION_ID() 连接ID,返回ID号,也是线程的ID
DATEBASE() 当前数据库
LAST_INSERT_ID() 最后插入记录的ID号
USER() 当前用户
VERSION() 版本信息
例1:
CONNECTION_ID()
查看当前用户连接的ID
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 1 |
+-----------------+
1 row in set (0.01 sec)
例2:查看当前数据库
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| test1017 |
+------------+
1 row in set (0.00 sec)
例3:查看最后插入字段的ID
现在的表
mysql> SELECT * FROM test;
+----+---------------+
| id | username |
+----+---------------+
| 1 | 巴西体育 |
| 2 | 阿根廷独立 |
| 3 | 基辅迪纳摩 |
| 4 | 巴西桑托斯 |
| 5 | 荷兰阿贾克斯 |
| 8 | AC Milan |
| 9 | 11 |
| 10 | 11 |
+----+---------------+
8 rows in set (0.01 sec)
插入字段
mysql> INSERT test(username) VALUES('sea');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 11 |
+------------------+
1 row in set (0.00 sec)
注意如果插入同时多条记录,那么返回这次插入的最前面的字段的ID
mysql> INSERT test(username) VALUES('ENGLAND'),('BRAZIL'),('ITALIA');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 12 |
+------------------+
1 row in set (0.00 sec)
现在的表
mysql> SELECT * FROM test;
+----+---------------+
| id | username |
+----+---------------+
| 1 | 巴西体育 |
| 2 | 阿根廷独立 |
| 3 | 基辅迪纳摩 |
| 4 | 巴西桑托斯 |
| 5 | 荷兰阿贾克斯 |
| 8 | AC Milan |
| 9 | 11 |
| 10 | 11 |
| 11 | sea |
| 12 | ENGLAND |
| 13 | BRAZIL |
| 14 | ITALIA |
+----+---------------+
12 rows in set (0.00 sec)
例4:打印当前用户
mysql> SELECT USER();
+--------+
| USER() |
+--------+
| root@ |
+--------+
1 row in set (0.00 sec)
例5:打印版本信息
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.5.28 |
+-----------+
1 row in set (0.00 sec)