MySQL 5.5常用信息函数有哪些

发布时间:2021-11-06 11:01:13 作者:小新
来源:亿速云 阅读:104

这篇文章主要介绍了MySQL 5.5常用信息函数有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

CONNECTION_ID()
显示连接ID(线程ID)

mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
|              50 |
+-----------------+
1 row in set (0.00 sec)

CURRENT_USER()
显当前客户端连接的用户名和主机名

mysql> SELECT CURRENT_USER();
+------------------+
| CURRENT_USER()   |
+------------------+
| system@localhost |
+------------------+
1 row in set (0.00 sec)

DATABASE()
显示当前连接的数据库名称

mysql> SELECT DATABASE();
+--------------------+
| DATABASE()         |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

FOUND_ROWS()
显示SELECT语句的返回行数,忽略LIMIT语句,在存储过程里面很有用。
SQL_CALC_FOUND_ROWS告诉MySQL计算结果集中的行数,忽略LIMIT语句,行数可以通过SELECT FOUND_ROWS()来查询出

mysql> select * from dept;
+--------+------------+----------+
| DEPTNO | DNAME      | LOC      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)

mysql> SELECT SQL_CALC_FOUND_ROWS * FROM dept ORDER BY 1 limit 2;
+--------+------------+----------+
| DEPTNO | DNAME      | LOC      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
+--------+------------+----------+
2 rows in set (0.00 sec)

mysql> SELECT FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)

ROW_COUNT()
在MySQL 5.5.5版本之前,ROW_COUNT()返回上一条UPDATE, DELETE,或INSERT语句的行数,对于其他语句,这个返回值没有意义。
在MySQL 5.5.5版本,ROW_COUNT()返回下列值:
DDL语句,例如CREATE TABLE 或 DROP TABLE:0。
DML语句,例如UPDATE, INSERT, 或 DELETE,ALTER TABLE 、 LOAD DATA INFILE和SELECT * FROM table_name INTO OUTFILE 'file_name':实际影响的行。
SELECT语句:-1
SIGNAL 语句: 0

mysql> select * from t20;
+------+
| id   |
+------+
|  200 |
|  100 |
+------+
2 rows in set (0.28 sec)

mysql> insert into t20 select * from t20;
Query OK, 2 rows affected (0.20 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

SCHEMA()
显示连接的数据库名称
mysql> SELECT SCHEMA();
+----------+
| SCHEMA() |
+----------+
| fire     |
+----------+
1 row in set (0.00 sec)

USER()、SESSION_USER()、SYSTEM_USER()
显当前客户端连接的用户名和主机名

mysql> SELECT SESSION_USER();
+------------------+
| SESSION_USER()   |
+------------------+
| system@localhost |
+------------------+
1 row in set (0.00 sec)

mysql> SELECT SYSTEM_USER();
+------------------+
| SYSTEM_USER()    |
+------------------+
| system@localhost |
+------------------+
1 row in set (0.00 sec)

mysql> SELECT USER();
+------------------+
| USER()           |
+------------------+
| system@localhost |
+------------------+
1 row in set (0.00 sec)

VERSION()
显示数据库版本

mysql> SELECT VERSION();
+------------+
| VERSION()  |
+------------+
| 5.5.48-log |
+------------+
1 row in set (0.00 sec)

LAST_INSERT_ID()
自增字段执行上一次的INSERT语句的值

mysql> create table test(id int auto_increment not null primary key, name varchar(15));
Query OK, 0 rows affected (0.08 sec)

mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(15) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> insert into test(name) values('Neo');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test(name) values('Lily');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+------+
| id | name |
+----+------+
|  1 | Neo  |
|  2 | Lily |
+----+------+
2 rows in set (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)

mysql> insert into test(name) values('Trinity');
Query OK, 1 row affected (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                3 |
+------------------+
1 row in set (0.00 sec)

感谢你能够认真阅读完这篇文章,希望小编分享的“MySQL 5.5常用信息函数有哪些”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. Linux mysql 安装无法启动 /etc 下无 mysql 配置文件问题处理
  2. 部署mysql5.5+版本与mysql5.5之前版本主从有哪些差异

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql

上一篇:总结使用Spring BeanUtils避免各种诡异的属性拷贝问题

下一篇:MySQL中Galera cluster集群常用参数有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》