Information Functions

BENCHMARK(count,expr) The BENCHMARK() function executes the expression expr repeatedly count times. It may be used to time how fast MySQL processes the expression. The result value is always 0. The intended use is in the mysql client, which reports query execution times:
    mysql> SELECT BENCHMARK(1000000,ENCODE("hello","goodbye"));
    +----------------------------------------------+
    | BENCHMARK(1000000,ENCODE("hello","goodbye")) |
    +----------------------------------------------+
    |                                            0 |
    +----------------------------------------------+
    1 row in set (4.74 sec)
    
The time reported is elapsed time on the client end, not CPU time on the server end. It may be advisable to execute BENCHMARK() several times, and interpret the result with regard to how heavily loaded the server machine is.
CHARSET(str) Returns the character set of the string argument.
    mysql> SELECT CHARSET(_utf8'abc');
            -> 'utf8'
    
CHARSET() was added in version 4.1.0.
COERCIBILITY(str) Returns the collation coercibility value of the string argument.
    mysql> SELECT COERCIBILITY('abc' COLLATE latin1_swedish_ci);
            -> 0
    mysql> SELECT COERCIBILITY('abc');
            -> 3
    mysql> SELECT COERCIBILITY(USER());
            -> 2
    
The return values have the following meanings:
0Explicit collation
1No collation
2Implicit collation
3Coercible
COLLATION(str) Returns the collation for the character set of the string argument.
    mysql> SELECT COLLATION('abc');
            -> 'latin1_swedish_ci'
    mysql> SELECT COLLATION(_utf8'abc');
            -> 'utf8_general_ci'
    
COLLATION() was added in version 4.1.0.
 
CONNECTION_ID() Returns the connection ID (thread ID) for the connection. Every connection has its own unique ID:
    mysql> SELECT CONNECTION_ID();
            -> 23786
    
 
CURRENT_USER() Returns the username and hostname that the current session was authenticated as. This value corresponds to the account that is used for assessing your access privileges. It may be different than the value of USER().
    mysql> SELECT USER();
            -> 'davida@localhost'
    mysql> SELECT * FROM mysql.user;
            -> ERROR 1044: Access denied for user: '@localhost' to database 'mysql'
    mysql> SELECT CURRENT_USER();
            -> '@localhost'
    
The example illustrates that although the client specified a username of davida (as indicated by the value of the USER() function), the server authenticated the client using an anonymous user account (as seen by the empty username part of the CURRENT_USER() value). One way this might occur is that there is no account listed in the grant tables for davida.
 
DATABASE() Returns the current database name:
    mysql> SELECT DATABASE();
            -> 'test'
    
If there is no current database, DATABASE() returns NULL as of MySQL 4.1.1, and the empty string before that.
 
FOUND_ROWS() A SELECT statement may include a LIMIT clause to restrict the number of rows the server returns to the client. In some cases, it is desirable to know how many rows the statement would have returned without the LIMIT, but without running the statement again. To get this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT statement, then invoke FOUND_ROWS() afterward:
    mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name
           WHERE id > 100 LIMIT 10;
    mysql> SELECT FOUND_ROWS();
    
The second SELECT will return a number indicating how many rows the first SELECT would have returned had it been written without the LIMIT clause. (If the preceding SELECT statement does not include the SQL_CALC_FOUND_ROWS option, then FOUND_ROWS() may return a different result when LIMIT is used than when it is not.) Note that if you are using SELECT SQL_CALC_FOUND_ROWS ... MySQL has to calculate how many rows are in the full result set. However, this is faster than running the query again without LIMIT, because the result set need not be sent to the client. SQL_CALC_FOUND_ROWS and FOUND_ROWS() can be useful in situations when you want to restrict the number of rows that a query returns, but also determine the number of rows in the full result set without running the query again. An example is a web script that presents a paged display containing links to the pages that show other sections of a search result. Using FOUND_ROWS() allows you to determine how many other pages are needed for the rest of the result. The use of SQL_CALC_FOUND_ROWS and FOUND_ROWS() is more complex for UNION queries than for simple SELECT statements, because LIMIT may occur at multiple places in a UNION. It may be applied to individual SELECT statements in the UNION, or global to the UNION result as a whole. The intent of SQL_CALC_FOUND_ROWS for UNION is that it should return the row count that would be returned without a global LIMIT. The conditions for use of SQL_CALC_FOUND_ROWS with UNION are:
  • The SQL_CALC_FOUND_ROWS keyword must appear in the first SELECT of the UNION.

  • The value of FOUND_ROWS() is exact only if UNION ALL is used. If UNION without ALL is used, duplicate removal occurs and the value of FOUND_ROWS() is only approximate.

  • If no LIMIT is present in the UNION, SQL_CALC_FOUND_ROWS is ignored and returns the number of rows in the temporary table that is created to process the UNION.

SQL_CALC_FOUND_ROWS and FOUND_ROWS() are available starting at MySQL version 4.0.0.
 
LAST_INSERT_ID([expr]) Returns the last automatically generated value that was inserted into an AUTO_INCREMENT column.
    mysql> SELECT LAST_INSERT_ID();
            -> 195
    
The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions. The value of LAST_INSERT_ID() is not changed if you update the AUTO_INCREMENT column of a row with a non-magic value (that is, a value that is not NULL and not 0). If you insert many rows at the same time with an insert statement, LAST_INSERT_ID() returns the value for the first inserted row. The reason for this is to make it possible to easily reproduce the same INSERT statement against some other server. If expr is given as an argument to LAST_INSERT_ID(), then the value of the argument is returned by the function, and is set as the next value to be returned by LAST_INSERT_ID(). This can be used to simulate sequences: First create the table:
    mysql> CREATE TABLE sequence (id INT NOT NULL);
    mysql> INSERT INTO sequence VALUES (0);
    
Then the table can be used to generate sequence numbers like this:
    mysql> UPDATE sequence SET id=LAST_INSERT_ID(id+1);
    
You can generate sequences without calling LAST_INSERT_ID(), but the utility of using the function this way is that the ID value is maintained in the server as the last automatically generated value (multi-user safe). You can retrieve the new ID as you would read any normal AUTO_INCREMENT value in MySQL. For example, LAST_INSERT_ID() (without an argument) will return the new ID. The C API function mysql_insert_id() can also be used to get the value. Note that mysql_insert_id() is only updated after INSERT and UPDATE statements, so you can't use the C API function to retrieve the value for LAST_INSERT_ID(expr) after executing other SQL statements like SELECT or SET. See mysql_insert_id().
 
SESSION_USER()SESSION_USER() is a synonym for USER().  
SYSTEM_USER()SYSTEM_USER() is a synonym for USER().  
USER() Returns the current MySQL username and hostname:
    mysql> SELECT USER();
            -> 'davida@localhost'
    
The value indicates the username you specified when connecting to the server, and the client host from which you connected. (Prior to MySQL Version 3.22.11, the function value does not include the client hostname.) You can extract just the username part, regardless of whether the value includes a hostname part, like this:
    mysql> SELECT SUBSTRING_INDEX(USER(),'@',1);
            -> 'davida'
    
 
VERSION() Returns a string indicating the MySQL server version:
    mysql> SELECT VERSION();
            -> '3.23.13-log'
    
Note that if your version ends with -log this means that logging is enabled.