1 Time format conversion DATE_FORMAT(field, time format) SELECT DATE_FORMAT(time,'%Y-%m-%d') FROM table;
2 Time converted into seconds TIME_TO_SEC(field) SELECT TIME_TO_SEC(time) FROM table ;
3 Convert seconds into time SEC_TO_TIME(field) SELECT SEC_TO_TIME(time) FROM table ;
4 Query today's data TO_DAYS(field) = TO_DAYS(now()); SELECT * FROM table WHERE TO_DAYS(time) = TO_DAYS(now());
5 Query the data of yesterday TO_DAYS(NOW())-TO_DAYS(field)=1 SELECT * FROM table WHERE TO_DAYS(NOW())-TO_DAYS(time)=1;
6 Query the data of the last few days DATE_SUB(CURDATE(), INTERVAL days DAY) <= DATE(field) Query the data of the last 7 days: SELECT * FROM table WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= DATE(time);
7 Query this week's data YEARWEEK(date_format(field ,'%Y-%m-%d')) = YEARWEEK(now()) select * from table WHERE YEARWEEK(date_format(time,'%Y-%m-%d')) = YEARWEEK(now()) ;
8 Query last week's data YEARWEEK(date_format(field,'%Y-%m-%d')) = YEARWEEK(now())-1 select * from table WHERE YEARWEEK(date_format(time,'%Y-%m -%d')) = YEARWEEK(now())-1;
9 Query this month's data DATE_FORMAT(field,'%Y-%m')=DATE_FORMAT(now(),%Y-%m) select * from table WHERE date_format(time,'%Y-%m')=date_format(now(),%Y-%m)
10 Query the data of the previous month DATE_FORMAT(field,'%Y-%m')=DATE_FORMAT(DATE_SUB (curdate(), INTERVAL 1 MONTH),'%Y-%m') SELECT * FROM table WHERE DATE_FORMAT(time,'%Y-%m')=DATE_FORMAT(DATE_SUB(curdate(), INTERVAL 1 MONTH),' %Y-%m')
string function
ASCII(char) returns the ASCII code value of the character
BIT_LENGTH(str) returns the bit length of the string
CONCAT(s1,s2...,sn) will s1,s2...,sn Concatenate into a string
CONCAT_WS(sep,s1,s2...,sn) Concatenate s1,s2...,sn into a string and separate
INSERT(str,x,y, instr) Replace the string str starting at the xth position with a substring y characters long with the string instr, and return the result
FIND_IN_SET(str,list) Analyze a comma-separated list of lists, and if str is found, return the position of str in the list
LCASE(str) or LOWER(str) Return the result of changing all characters in the string str to lowercase
LEFT(str ,x) returns the leftmost x characters in the
string str LENGTH(s) returns the number of characters in the string str
LTRIM(str) cuts off leading spaces from the string str
POSITION(substr,str) returns the substring substr
QUOTE(str) Escape the single quote in str with a backslash at the position of the first occurrence in the string str
REPEAT(str,srchstr,rplcstr) Returns the result of repeating the string str x times
REVERSE(str) Returns the reversed character The result of the string str
RIGHT(str,x) returns the rightmost x characters in the string str
RTRIM(str) returns the space at the end of the string str
STRCMP(s1,s2) compares the strings s1 and s2
TRIM(str) removes characters All spaces at the beginning and end of the string
UCASE(str) or UPPER(str) Returns the result of converting all characters in the string str to uppercase
Date and time functions
CURDATE() or CURRENT_DATE() Returns the current date
CURTIME() or CURRENT_TIME () returns the current time
DATE_ADD(date,INTERVAL int keyword) Returns the result of date plus interval time int (int must be formatted according to the keyword), such as: SELECTDATE_ADD(CURRENT_DATE,INTERVAL 6 MONTH);
DATE_FORMAT(date,fmt) According to the specified fmt Format format date date value
DATE_SUB(date, INTERVAL int keyword) Returns the result of date plus interval time int (int must be formatted according to the keyword), such as: SELECTDATE_SUB(CURRENT_DATE, INTERVAL 6 MONTH);
DAYOFWEEK(date) Returns the day of the week represented by date (1~7)
DAYOFMONTH(date) Returns the day of the month where date is (1~31)
DAYOFYEAR(date) Returns the day of the year where date is (1 ~366)
DAYNAME(date) returns the week name of date, such as: SELECT DAYNAME(CURRENT_DATE);
FROM_UNIXTIME(ts,fmt) According to the specified fmt format, format the UNIX timestamp ts
HOUR(time) Return the hour value of time (0 ~23)
MINUTE(time) returns the minute value of time (0~59)
MONTH(date) returns the month value of date (1~12)
MONTHNAME(date) returns the month name of date, such as: SELECT MONTHNAME(CURRENT_DATE);
NOW () returns the current date and time
QUARTER(date) returns the quarter (1~4) of the year in the year, such as SELECT QUARTER(CURRENT_DATE);
WEEK(date) returns the date as the week of the year (0~53)
YEAR(date) returns the date The year of date (1000~9999)
logical judgment statement
CASE WHEN[test1] THEN [result1]...ELSE [default] END If testN is true, return resultN, otherwise return default
CASE [test] WHEN[val1] THEN [ result]...ELSE [default]END If test and valN are equal, return resultN, otherwise return default
IF(test,t,f) If test is true, return t; otherwise return f
IFNULL(arg1,arg2) if arg1 Not empty, return arg1, otherwise return arg2
NULLIF(arg1,arg2) If arg1=arg2 return NULL; otherwise return arg1
case when use
select id,age, case username when 'gy_dev' then 'admin' when 'sunjf' then 'normal user' else 'Low level user' end as username from t_user group by case statement order by case statement
Formatting function
DATE_FORMAT(date,fmt) Formats a date value according to the string fmt
FORMAT(x,y) Formats x as a sequence of numbers separated by commas, y is the number of decimal places in the result
INET_ATON(ip) returns IP The digital representation of the address
INET_NTOA(num) Returns the IP address represented by the number
TIME_FORMAT(time,fmt) Formats the time value according to the string fmt
System information function
DATABASE() Returns the current database name
BENCHMARK(count,expr) Converts the expression expr Repeatedly run count times
CONNECTION_ID() Returns the connection ID of the current client
FOUND_ROWS() Returns the total number of rows retrieved by the last SELECT query
USER() or SYSTEM_USER() Returns the current login user name
VERSION() Returns the version of the MySQL server