Table of contents
date function
Get hours, minutes and seconds:
select current_time();+----------------+| current_time() |+----------------+| 13 :51:21 |+----------------+date is dateGet the timestamp:select current_timestamp();+---------------------+| current_timestamp() |+---------------------+| 2017 - 11 - 19 13 :51:48 |+---------------------+
string functions
length returns bytes.
Get the character set of the ename column of the emp table
select charset(ename) from EMP;
It is required to display the information in the exam_result table, and the display format is: "XXX has XXX points for Chinese , XXX points for mathematics, and XXX points for English "
select concat(name, ' Chinese is ' ,chinese, ' fen, mathematics is ' ,math, ' fen ' ) as ' score ' from student;Find the number of bytes occupied by the student name in the student table
select length(name), name from student;
Note: The length function returns the length of the string in bytes. If it is a multibyte character, calculate the number of bytes;Single-byte characters count as one byte. For example: letters and numbers are counted as one byte, and Chinese means multiple bytes(related to character set encoding)Replace all names with S in the EMP table with ' Shanghai '
select replace(ename, 'S', '上海') ,ename from EMP;
Intercept the second to third characters of the ename field in the EMP table
select substring(ename, 2, 2), ename from EMP;
Display all employee names in lowercase
select concat(lcase(substring(ename, 1, 1)),substring(ename,2)) from EMP;
math function
absolute value
select abs(-100.2);
Rounded up
select ceiling(23.04);
round down
select floor(23.7);
Keep 2 decimal places (decimals are rounded )
select format(12.3456, 2);
Generate random numbers
select rand();
other functions
user() queries the current userselect user();md5(str) performs an md5 digest on a string, and obtains a 32- bit string after the digestselect md5( 'admin' )+----------------------------------+| md5( 'admin' ) |+----------------------------------+| 21232 f297a57a5a743894a0e4a801fc3 |+----------------------------------+database() displays the database currently in useselect database();password() function, MySQL database uses this function to encrypt usersselect password( 'root' );+-------------------------------------------+| password( 'root' ) |+-------------------------------------------+| * 81 F5E21E35407D884A6CD4A731AEBFB6AF209E1B |+-------------------------------------------+ifnull ( val1 , val2 ) returns val2 if val1 is null , otherwise returns the value of val1select ifnull( 'abc' , '123' );+----------------------+| ifnull( 'abc' , '123' ) |+----------------------+| abc |+----------------------+1 row in set ( 0.01 sec)select ifnull( null , '123' );+---------------------+| ifnull( null , '123' ) |+---------------------+| 123 |+---------------------+1 row in set ( 0.00 sec)