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)
顯示連線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)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2108479/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 常用函式MySql函式
- MySQL 常用函式。MySql函式
- MySQL常用函式MySql函式
- Mysql 常用函式(1)- 常用函式彙總MySql函式
- Mysql 常用函式(15)- upper 函式MySql函式
- MySQL 5.5 建立儲存過程和函式MySql儲存過程函式
- MySQL常用函式彙總MySql函式
- mysql常用函式詳解MySql函式
- 轉MySQL--mysql常用函式打全MySql函式
- Mysql 常用函式(20)- ceiling 函式MySql函式
- MySQL 5.5 統計資訊收集MySql
- mysql 常用的九類函式MySql函式
- (4)mysql 中的常用函式MySql函式
- mysql常用函式彙總(分享)MySql函式
- mysql常用函式--個人筆記MySql函式筆記
- 《MySQL 入門教程》第 16 篇 MySQL 常用函式之日期函式MySql函式
- mysql之常用函式(核心總結)MySql函式
- Mysql中常用函式的使用示例MySql函式
- 《MySQL 入門教程》第 14 篇 MySQL 常用函式之數學函式MySql函式
- mysql5.5的一些函式_user()_current_user()MySql函式
- 第十章 MySQL 常用函式MySql函式
- 【實驗】【MySQL】MySQL常用函式“自助式”示例演示全過程MySql函式
- 常用函式函式
- 常用函式--時間函式函式
- MySQL 十大常用字串函式MySql字串函式
- MySQL 5.5 常用的複製環境管理命令MySql
- mysql注入方法邏輯運算及常用函式MySql函式
- Mysql中常用函式 分組,連線查詢MySql函式
- PHP 常用函式PHP函式
- PHP常用函式PHP函式
- js 常用函式JS函式
- 常用函式集合函式
- 常用助手函式函式
- 常用函式整理函式
- jQuery常用函式jQuery函式
- SqlServer常用函式SQLServer函式
- js常用函式JS函式
- ORACLE 常用 函式Oracle函式