A complete list of commonly used functions in MySQL

1. String functions

Commonly used functions:

function Function
CONCAT(s1, s2, …, sn) String concatenation, concatenate s1, s2, …, sn into a string
LOWER(str) Convert all strings to lowercase
UPPER(str) Convert all strings to uppercase
LPAD(str, n, pad) Left padding, use string pad to pad the left side of str to reach n string lengths
RPAD(str, n, pad) Right padding, fill the right side of str with string pad to reach n string length
TRIM(str) Remove leading and trailing spaces from string
SUBSTRING(str, start, len) Returns a string of length len from the start position of string str
REPLACE(str, old_substring, new_substring) Replacement string, str: The string to be replaced, old_substring: The substring to be replaced, new_substring: The new substring after replacement.

 Case:

Due to changes in business needs, the job numbers of corporate employees have been unified into 5 digits. Currently, all numbers with less than 5 digits are filled with zeros in front. For example: the job number of employee No. 1 should be 00001. The table name is emp, and the employee number field is workno.

SQL is written as follows: update emp set workno = lpad (workno,5,'0');

 

2. Numerical functions

 

Common functions:

function Function
CEIL(x) Rounded up
FLOOR(x) Round down
MOD(x, y) Returns the modulus of x/y
RAND() Returns a random number between 0 and 1
ROUND(x, y) Find the rounded value of parameter x, retaining y decimal places

3. Date function

Commonly used functions:

function Function
CURDATE() Return current date
CURTIME() Return current time
NOW() Returns the current date and time
YEAR(date) Get the year of the specified date
MONTH(date) Get the month of the specified date
DAY(date) Get the date of the specified date
DATE_ADD(date, INTERVAL expr type) Returns a date/time value plus a time value expr
DATEDIFF(date1, date2) Returns the number of days between the start time date1 and the end time date2

Case:

        Query the number of days of employment for all employees and sort them in descending order according to the number of days of employment. The table name is emp, the employee's entry day field is entrydate, and the SQL statement is as follows:
        select name, datediff(curdate(), entrydate) as 'entrydays' from emp order by entrydays desc;

4. Process function

Commonly used functions:

function Function
IF(value, t, f) If value is true, return t, otherwise return f
IFNULL(value1, value2) If value1 is not empty, return value1, otherwise return value2
CASE WHEN [ val1 ] THEN [ res1 ] … ELSE [ default ] END If val1 is true, return res1,... otherwise return default value
CASE [ expr ] WHEN [ val1 ] THEN [ res1 ] … ELSE [ default ] END If the value of expr is equal to val1, return res1,... Otherwise, return the default value

 Case:

The scores of each student in the class are counted, and the display rules are as follows: >= 85, showing excellence. >= 60, the display is passed. Otherwise, the display fails. The SQL statement is as follows:

select
id,name,
(case when math >= 85 then'excellent' when math >=60 then'pass' else 'fail' end )'mathematics' (case when english >= 85 then'excellent' when english >
= 60 then 'pass' else 'fail' end ) 'English', (case when chinese >= 85 then 'excellent' when chinese >=60 then 'pass' else 'fail' end ) 'Chinese' from score;

5. Aggregation function

Grammar:
  SELECT 聚合函数(字段列表) FROM 表名;
Example:
  SELECT count(id) from employee where workaddress = "广东省"; 

Guess you like

Origin blog.csdn.net/weixin_55772633/article/details/132119968