mysql common function backup

1. The string function
ascii (str)  
returns the ascii value of the first character of the string str (returns 0 when str is an empty string) 
mysql> select ascii('2'); 
  -> 50 
mysql> select ascii(2) ; 
  -> 50 
mysql> select ascii('dete'); 
  -> 100 

 
ord(str)  
returns the same value as the ascii() function returns if the beginning of the string str is a single byte.
 
If it is a multi-byte character, return in the format ((first byte ascii code)*256+(second byte ascii code))[*256+third byte asciicode...] mysql> select ord('2') 

  - > 50  
  
conv(n,from_base,to_base)  
converts the number n base and returns it as a string (null is returned when any parameter is null, the base range is 2-36 base, when to_base is a negative number, n is used as a valid number) Signed number otherwise unsigned number, conv works with 64-bit precision) 
mysql> select conv("a",16,2); 
  -> '1010'
mysql> select conv("6e",18,8); 
  - > '172'
mysql> select conv(-17,10,-18); 
  -> '-h'
mysql> select conv(10+"10"+'10'+0xa,10,10); 
  -> '40 ' 
  
bin(n)  
converts n into a binary value and returns it as a string (n is a bigint number, equivalent to conv(n,10, 


 
 
Convert n to octal value and return it as a string (n is a bigint number, equivalent to conv(n,10,8)) 
mysql> select oct(12); 
  -> '14' 
  
hex(n)  
convert n to Hexadecimal and returned as a string (n is a bigint number, equivalent to conv(n,10,16)) 
mysql> select hex(255); 
  -> 'ff' 
  
char(n,...)  
returned by A string composed of ASCII code characters corresponding to parameters n,... (parameters are n,... are numeric sequences, and null values ​​are skipped)  
mysql> select char(77,121,83,81,'76'); 
  -> 'mysql'
mysql> select char(77,77.3,'77.3'); 
  -> 'mmm' 
  
concat(str1, str2,...)  
concatenates the parameters into a long string and returns it (when any parameter is null Return null) 
mysql> select concat('my', 's', 'ql'); 
  -> 'mysql'
mysql> select concat('my', null, 'ql'); 
  -> null
mysql> select concat(14.3); 
  -> '14.3' 
 
length(str)  
octet_length(str) 
char_length(str) 
character_length(str) 
returns the length of the string str (for multi-byte characters char_length is only calculated once)
mysql> select length('text'); 
  -> 4 
mysql> select octet_length('text' ); 
  -> 4  
 
locate(substr,str)  
position(substr in str)  
returns the position where the string substr appears for the first time in the string str (returns 0 when str does not contain substr) 
mysql> select locate('bar', ' foobarbar'); 
  -> 4 
mysql> select locate('xbar', 'foobar'); 
  -> 0  
   
locate(substr, str, pos) 
returns the first occurrence of the string substr starting from the pos position of the string str position (returns 0 when str does not contain substr) 
mysql> select locate('bar', 'foobarbar',5); 
  -> 7  
 
instr(str,  
substr) returns the position of the first occurrence of string substr in string str (returns 0 when str does not contain substr) 
mysql> select instr('foobarbar', 'bar'); 
  -> 4 
mysql> select instr('xbar', 'foobar'); 
  -> 0   
 
lpad(str,len,padstr)  
fills the left end of str with the string padstr until The length of the string is len and returns 
mysql> select lpad('hi',4,'??'); 
  -> '??hi' 
  
rpad(str,len,padstr)  
fills the right end of str with the string padstr until the length of the string is len and returns 
mysql> select rpad('hi',5,'?'); 
  -> 'hi???' 
 
left(str,len)  
returns the left end of the string str len characters 
mysql> select left('foobarbar ', 5); 
  -> 'fooba' 
 
right(str,len)  
returns len characters from the right end of the string str  
mysql> select right('foobarbar', 4); 
  -> 'rbar' 
 
substring(str,pos,len)  
substring(str from pos for len)  
mid(str,pos,len)  
Returns the position of string str starting from pos len characters mysql> select substring('quadratically',5,6); -> 
  'ratica' 
 
substring(str,pos)  
substring(str from pos)  
returns the position of string str starting from pos A substring of 
mysql> select substring('quadratically',5); 
  -> 'ratically'
mysql> select substring('foobarbar' from 4); 
  -> 'barbar' 
 
substring_index(str,delim,count)  
returns the string from The substring after the delimiter delim that appears in the countth occurrence of str
(the left end is returned when count is a positive number, otherwise the right end substring is returned) 
mysql> select substring_index('www.mysql.com', '.', 2); 
  - > 'www.mysql'
mysql> select substring_index('www.mysql.com', '.', -2); 
  -> 'mysql. com'
 
ltrim(str)  
returns the string str with the left space removed 
mysql> select ltrim(' barbar'); 
  -> 'barbar' 
 
rtrim(str)  
returns the string str with the right space removed 
mysql> select rtrim('barbar '); 
  -> 'barbar' 
 
trim([[both | leading | trailing] [remstr] from] str)  
returns prefix or suffix remstr The deleted string str (positional parameters default to both, remstr default value is space) 
mysql> select trim(' bar '); 
  -> 'bar'
mysql> select trim (leading 'x' from 'xxxbarxxx'); 
  - > 'barxxx'
mysql> select trim(both 'x' from 'xxxbarxxx'); 
  -> 'bar'
mysql> select trim(trailing 'xyz' from 'barxxyz'); 
  -> 'barx' 
 
soundex(str)  
returns str A homophone string ("roughly the same" sounding string has the same
homophone string, non-alphanumeric characters are ignored, Letters outside az are treated as vowels)
mysql> select soundex('hello'); 
  -> 'h400'
mysql> select soundex('quadratically'); 
  -> 'q36324' 
  
space(n)  
returns a string consisting of n space characters 
mysql> select space(6); 
  -> ' ' 
  
replace(str, from_str, to_str)  
uses the string to_str to replace the string str in substring from_str and return 
mysql> select replace('www.mysql.com', 'w', 'ww'); - 
  > 'wwwwww.mysql.com' 
 
repeat(str, count)  
returns count strings str connected into a string (null
is returned when any parameter is null, and an empty string is returned when count<=0) 
mysql> select repeat('mysql', 3); 
  -> 'mysqlmysqlmysql' 
  
reverse(str)  
reverses the string str character order and return 
mysql> select reverse('abc'); 
  -> 'cba' 
 
insert(str,pos,len,  
newstr) Replace the substring of string str that is len characters long starting from position pos with the string
newstr and return 
mysql> select insert('quadratic', 3, 4, 'what'); 
  -> 'quwhattic' 
 
elt(n,str1,str2,str3,...)  
returns the nth string (n is less than 1 or greater than the number of parameters, returns null) 
mysql> select elt(1, 'ej', 'heja ', 'hej', 'foo'); 
  -> 'ej'
mysql> select elt(4, 'ej', 'heja', 'hej', 'foo'); - 
  > 'foo' 
 
field(str,str1 , str2, str3,...)  
returns str equal to the serial number of the nth string following it (returns 0 if str is not found) 
mysql> select field('ej', 'hej', 'ej', 'heja' , 'hej',
'foo'); 
  -> 2 
mysql> select field('fo', 'hej', 'ej', 'heja', 'hej', '
foo'); 
  -> 0  
 
find_in_set(str,  
strlist) returns the serial number of str in the string set strlist (null is returned if any parameter is null
, 0 is returned if str is not found, and the operation is abnormal when parameter 1 contains ",") 
mysql> select find_in_set('b','a, b,c,d'); 
  -> 2  
  
make_set(bits,str1,str2,...) 
Convert the number of parameter 1 to binary. If the binary bit at a certain position is equal to 1, the
string at the corresponding position is selected into the string set and returned (the null string is not added to the result) 
mysql> select make_set(1,'a' ,'b','c'); 
  -> 'a'
mysql> select make_set(1 | 4,'hello','nice','world'); -> 
  'hello,world'
mysql> select make_set(0 ,'a','b','c'); 
  -> '' 
 
export_set(bits,on,off,[separator,[number_of_bits]])  
arranges the string set by bits, and only inserts the string when the bit is equal to 1 on, otherwise insert
off (separator default value ",", when the number_of_bits parameter is used, the length is insufficient to fill in 0
and is truncated if it is too long)  
mysql> select export_set(5,'y','n',',',4) 
  -> y ,n,y,   n
 
lcase(str) 
lower(str)  
returns the lowercase string str 
mysql> select lcase('quadratically'); 
  -> 'quadratically' 
  
ucase(str)  
upper(str)  
returns the uppercase string str 
mysql> select ucase('quadratically'); 
  -> 'quadratically' 
 
load_file(file_name)  
reads the file and returns the file content as a string (the file cannot be found, the path
is incomplete, there is no permission, and the length is greater than max_allowed_packet, null will be returned) 
mysql > update table_name set blob_column=load_file
("/tmp/picture") where id=1;  
 
2. The mathematical function
abs(n) 
returns the absolute value of n 
mysql> select abs(2);   
  -> 2   
mysql> select abs(- 32);   
  -> 32    
  
sign(n) 
returns the sign of the parameter (is -1, 0 or 1) 
mysql> select sign(-32);   
  -> -1   
mysql> select sign(0);   
  -> 0   
mysql> select sign(234);   
  -> 1    
 
mod(n,m)   
modulo operation, returns the remainder when n is divided by m (same as % operator)   
mysql> select mod(234, 10);   
  -> 4   
mysql> select 234 % 10;   
  -> 4   
mysql> select mod(29,9);   
  -> 2    
 
floor(n) 
returns the largest integer value not greater than n 
mysql> select floor(1.23);   
  -> 1   
mysql> select floor(-1.23);   
  -> -2    
 
ceiling(n) 
returns the smallest integer value not less than n 
mysql> select ceiling(1.23);   
  -> 2   
mysql> select ceiling( -1.23);   
  -> -1    
 
round(n,d) 
returns the rounded value of n, retaining d decimal places (the default value of d is 0) 
mysql> select round(-1.23);   
  -> -1   
mysql> select round( -1.58);   
  -> -2   
mysql> select round(1.58);   
  -> 2   
mysql> select round(1.298, 1);   
  -> 1.3   
mysql> select round(1.298, 0);   
  -> 1    
 
exp(n) 
returns the nth power of e (the base of natural logarithms) 
mysql> select exp(2 );   
  -> 7.389056   
mysql> select exp(-2);   
  -> 0.135335    
 
log(n) 
returns the natural logarithm of n 
mysql> select log(2);   
  -> 0.693147   
mysql> select log(-2);   
  -> null    
 
log10(n) 
returns the base 10 logarithm of n 
mysql> select log10(2);   
  -> 0.301030   
mysql> select log10(100);   
  -> 2.000000   
mysql> select log10(-100);   
  -> null    
 
pow(x ,y)   
power(x,y)   
 returns the value x raised to the y power 
mysql> select pow(2,2);   
  -> 4.000000   
mysql> select pow(2,-2);   
  -> 0.250000  sqrt(n)   returns the square root of a non-negative number n  mysql> select sqrt(4);      -> 2.000000    mysql> select sqrt(20);      -> 4.472136     pi()     returns   pi mysql> select pi();      -> 3.141593     cos(n)   returns the cosine value of n  mysql> select cos(pi());    -> -1.000000     sin(n )   returns the sine value of n   mysql> select sin(pi());      -> 0.000000     tan(n)  returns the tangent value of n  mysql> select tan(pi()+1);      -> 1.557408     acos(n)   returns the inverse of n Cosine (n is the cosine value, in the range of -1 to 1, otherwise null is returned) 
 






 




 




 




 




 


mysql> select acos(1);   
  -> 0.000000   
mysql> select acos(1.0001);   
  -> null   
mysql> select acos(0);   
  -> 1.570796    
 
asin(n) 
returns n inverse sine value 
mysql> select asin(0.2);   
  -> 0.201358   
mysql> select asin('foo');   
  -> 0.000000    
 
atan(n) 
returns the arc tangent of n 
mysql> select atan(2);   
  -> 1.107149   
mysql> select atan(-2);   
  -> -1.107149   
atan2(x,y)   
 returns the arctangent of two variables x and y (similar to the arctangent of y/x, the sign determines the quadrant) 
mysql> select atan(-2,2);   
  -> -0.785398   
mysql> select atan(pi (),0);   
  -> 1.    570796
 
cot(n) 
returns the cotangent of x 
mysql> select cot(12);   
  -> -1.57267341   
mysql> select cot(0);   
  -> null    
 
rand() 
rand(n)   
returns a random floating point value in the range 0 to 1.0 (you can use the number n as the initial value )
 
mysql> select rand();   
  -> 0.5925   
mysql> select rand(20);   
  -> 0.1811   
mysql> select rand(20);   
  -> 0.1811   
mysql> select rand();   
  -> 0.2079   
mysql> select rand();   
  -> 0.7888    
 
degrees(n) 
Convert n from radians to degrees and return 
mysql> select degrees(pi());   
  -> 180.000000    
 
radians(n) 
Convert n from degrees to radians and return  
mysql> select radians(90);   
  -> 1.570796    truncate(n,   d)   
 

Keep d decimal places of number n and return 
mysql> select truncate(1.223,1);   
  -> 1.2   
mysql> select truncate(1.999,1);   
  -> 1.9   
mysql> select truncate(1.999,0);   
  -> 1    
 
least( x,y,...)   
returns the minimum value (if the return value is used in an integer (real or size-sensitive string) context or all arguments are integers (real or size-sensitive string) then they are treated as integers (real or size-sensitive string) Sensitive string) comparison, otherwise the case-insensitive string is compared) 
mysql> select least(2,0);   
  -> 0   
mysql> select least(34.0,3.0,5.0,767.0);   
  -> 3.0   
mysql> select least("b","a","c");   
  -> "a"    
 
greatest(x,y,...)   
returns the maximum value (the rest are the same as least()) 
mysql> select greatest(2,0);   
  -> 2   
mysql> select greatest(34.0,3.0,5.0,767.   0);   
  -> 767.0   
mysql> select greatest("b","a","c");   
  -> "c"     3. The period time function dayofweek(date)    returns the date date is the day of the week (1=Sunday, 2=Monday,... 7=Saturday, odbc standard)  mysql> select dayofweek('1998-02-03');      -> 3     weekday(date) returns the day of the week date is (0=Monday, 1=Tuesday,...6=Sunday) . mysql> select weekday('1997-10-04 22:23:   
 





 
   

 




 
 



 




 

Returns the month value in date   
mysql> select month('1998-02-03');   
  -> 2    
 
dayname(date)   
returns the day of the week that date is (returned by English name) 
mysql> select dayname("1998-02-05" );   
  -> 'thursday'    
 
monthname(date)   
returns the month of date (returned by English name) 
mysql> select monthname("1998-02-05");   
  -> 'february'    
 
quarter(date)   
returns the year of date Which quarter   
mysql> select quarter('98-04-01');   
  -> 2    
 
week(date, first)  
returns the date in which week of the year (first default value is 0, first value 1 means Monday is
The beginning of the week, 0 starts from Sunday) 
mysql> select week('1998-02-20');   
  -> 7   
mysql> select week('1998-02-20',   0);   
  -> 7   
mysql> select week('1998-02-20',1);   
  -> 8    
 
year(date)   
returns the year of date (range is 1000 to 9999)   
mysql> select year('98-02-03');   
  -> 1998    
 
hour(time)   
returns the hour of time (range is 0 to 23) )  
mysql> select hour('10:05:03');   
  -> 10    
 
minute(time)   
returns the number of minutes of time (range is 0 to 59)   
mysql> select minute('98-02-03 10:05:03 ');   
  -> 5    
 
second(time)   
returns the number of seconds of time (range is 0 to 59)  
mysql> select second('10:05:03');   
  -> 3    
 
period_add(p,n)   
adds n months to Period p and returns (the format of p is yymm or yyyymm)   
mysql> select period_add(9801,2);   
  -> 199803    
 
period_diff(p1,p2)   
returns the number of months between periods p1 and p2 (the format of p1 and p2 is yymm or yyyymm ) 
mysql> select period_diff(9802,199703);   
  -> 11   
 
date_add(date,interval expr type) 
date_sub(date,interval expr type)   
adddate(date,interval expr type)   
subdate(date,interval expr type) 
add date and time Subtraction operations 
(adddate() and subdate() are synonyms of date_add() and date_sub(). You
can also use operators + and - instead of functions. 
date is a datetime or date value, and expr is a method for adding and subtracting dates. The expression
string type specifies how the expression expr should be interpreted 
 [type value meaning expected expr format]: 
 second seconds seconds   
 minute minutes minutes   
 hour time hours   
 day days days month months   
 months   
 year years years   
 minute_second minutes and seconds "minutes:seconds "   
 hour_minute hours and minutes "hours:minutes"   
 day_hour "days hours"   
 year_month "years-months"   
 hour_second "hours:minutes:seconds"   
 day_minute "days hours:minutes"   
 day_second day, hour, minute, second" days
hours:minutes:seconds"
 expr allows any punctuation as a separator. If all are date values, the result is a
date value, otherwise the result is a datetime value.) 
 If the type keyword is incomplete, mysql takes the value from the right end, day_second Because
the hour and minute are missing, they are equal to minute_second) 
 If you add month, year_month or year, and the number of days is greater than the maximum number of days in the result month,
the maximum number of days is used)   
mysql> select "1997-12-31 23:59:59" + interval 1 second; 
 
  - > 1998-01-01 00:00:   00   
mysql> select interval 1 day + "1997-12-31";   
  -> 1998-01-01   
mysql> select "1998-01-01" - interval 1 second;   
  -> 1997-12-31 23:59:59   
mysql> select date_add("1997-12-31 23:59:59",interval 1
second);   
  -> 1998-01-01 00:00:00   
mysql> select date_add("1997-12-31 23:59:59",interval 1
day);   
  -> 1998-01-01 23:59:59   
mysql> select date_add("1997-12-31 23:59:59",interval
"1:1" minute_second);   
  -> 1998-01-01 00:01:00   
mysql> select date_sub("1998-01-01 00:00:00",interval "1
1:1:1" day_second);   
  -> 1997-12-30 22:58:59   
mysql> select date_add("1998-01-01 00:00:00", interval "-1
10" day_hour); 
  -> 1997-12-30 14:00:00   
mysql> select date_sub("1998-01-02", interval 31 day);   
  -> 1997-12-02   
mysql> select extract(year from "1999-07-02");   
  -> 1999   
mysql> select extract(year_month from "1999-07-02
01:02:03");   
  -> 199907   
mysql> select extract(day_minute from "1999-07-02
01:02:03");   
  -> 20102    
 
to_days(date)   
returns the number of days since AD ​​0 (not counting years before 1582) 
mysql> select to_days(950501);   
  -> 728779   
mysql > select to_days('1997-10-07');   
  -> 729669    
 
from_days(n)   
 gives the number of days to return from year 0 to the present (not counting years before 1582)  
mysql> select from_days(729669);   
  -> '1997 -10-07'    
 
date_format(date,   
format)  Format the date value according to the format string 
 (available identifiers in the format string: 
 %m month name (january...december)   
 %w week name (sunday...saturday)   
 %d day of the month with English prefix (1st, 2nd, 3rd, etc.)   
 %y year, number, 4 digits   
 %y Year, number, 2 digits   
 %a abbreviated name of the week (sun...sat)   
 %d number of days in the month, number (00...31)   
 %e number of days in the month, number (0...31)   
 %m month, Number (01...12)   
 %c month, number (1...12)   
 %b abbreviated month name (jan...dec)   
 %j number of days in a year (001...366)   
 %h hour (00... 23)   
 %k hours (0...23)   
 %h hours (01...12)   
 %i hours (01...12)   
 %l hours (1...12)   
 %i minutes, numbers (00...59)   
 % r Time, 12 hours (hh:mm:ss [ap]m)   
 %t Time, 24 hours (hh:mm:ss)   
 %s seconds (00……59)   
 %s seconds (00……59)   
 %p am or pm   
 %w The number of days in a week (0=sunday...6=saturday)   
 %u The week (0...52), where Sunday is the first day of the week   
 %u The week (0...52), where Monday is the day of the week The first day   
 %% character %) 
mysql> select date_format('1997-10-04 22:23:00','%w %m %
y');   
  -> 'saturday october 1997'   
mysql> select date_format(' 1997-10-04 22:23:00','%h:%i:%
s');   
  -> '22:23:00'   
mysql> select date_format('1997-10-04 22:23:00' ,'%d %y %a
%d %m %b %j');   
  -> '4th 97 sat 04 10 oct 277'   
mysql> select date_format('1997-10-04 22:23:00','% h %k %i
%r %t %s %w');   
  -> '22 22 10 10:23:00 pm 22:23:00 00 6'    
 
time_format(time, 
format)  is similar to date_format(), but time_format only handles hours, minutes, and seconds (other
symbols produce a null value or 0)  
 
curdate()    
current_date()  returns the current date value in 'yyyy-mm-dd' or yyyymmdd format ( either a string or a number depending on the context   
 of the return value ) mysql> select curdate();      -> '1997-12-15'    mysql> select curdate() + 0;      -> 19971215     curtime()    current_time()   returns the current time value in 'hh:mm:ss' or hhmmss format (the return value is a string or a number depending on the context )      mysql> select curtime();      -> '23:50:26'    mysql> select curtime() + 0;      -> 235026     now()   sysdate()   current_timestamp()   in 'yyyy-mm-dd hh:mm:ss' or yyyymmddhhmmss format Returns the current date and time (either a string or a number depending on the context of the return value)     mysql> select now();      -> '1997-12-15 23:50:   26'   





 








 
 
 





mysql> select now() + 0;   
  -> 19971215235026    
 
unix_timestamp()   
unix_timestamp(date)   
returns a unix timestamp (the number of
seconds , the default value of date is the current time
mysql> select unix_timestamp();   
  -> 882226357   
mysql> select unix_timestamp('1997-10-04 22:23:00');   
  -> 875996580    
 
from_unixtime(unix_timestamp)   
with 'yyyy-mm-dd hh:mm:ss' Or return
the value of the timestamp in yyyymmddhhmmss format (it is a string or a number according to the context of the return value)    
mysql> select from_unixtime(875996580);   
  -> '1997-10-04 22:23:00'   
mysql> select from_unixtime(875996580) + 0;   
  -> 19971004222300    
 
from_unixtime(unix_timestamp,   
format) returns the value of the timestamp in format string format 
mysql> select from_unixtime(unix_timestamp(),'%y %d %m %
h:%i:%s %x');   
  -> '1997 23rd December 03:43:30 x'    
 
sec_to_time(seconds)   
with 'hh: mm:ss' or hhmmss format returns the time value converted from seconds (the return value is a string or a number depending on the context)    
mysql> select sec_to_time(2378);   
  -> '00:39:38'   
mysql> select sec_to_time( 2378) + 0;   
  -> 3938    
 
time_to_sec(time)   
returns the number of seconds in the time value   
mysql> select time_to_sec('22:23:00');   
  -> 80580   
mysql> select time_to_sec('00:39:   38');
  -> 2378 
 
conversion function
cast
usage: cast (field as data type) [Of course, whether the conversion can be successful depends on the issues that need to be paid attention to when the data type is forced to be converted]
Example: select cast (a as unsigned) as b from cardserver where order by b desc;
convert:
Usage: convert (field, data type)
Example: select convert (a, unsigned) as b from carderver where order by b desc;

Source: https://blog.csdn.net/qq_42239765/article/details/83898664

Guess you like

Origin blog.csdn.net/qq_37511875/article/details/102496203