mySQL learning introductory tutorial - 4. Built-in functions

Fourth, built-in functions:

  Including string functions, numerical functions, date functions, flow control functions, other functions (obtaining database information)...

 

1. String function [commonly used, need to be mastered]
1. concat(s1,s2,...,sn) #Concatenate the incoming parameters into a string
select concat('abc','def');
select concat(name,' age is ',age) from users;


2. insert(str,m,n,inser_str) #Replace the n characters of str starting at position m with insert_str
select insert('abcdef',2,3,'123456');
select insert(name,3, 2,'HAHA') from users;
select insert(name,2,2,'00') from users;


3. lower(str)/upper(str) #Convert the string str to lowercase/uppercase
select lower('HELLO'), upper('hello');
select lower('HELLO') as 'HELLO', upper( 'hello')as 'HELLO';
select * from users where upper(name) = 'AAA';


4. left(str,n)/right(str,n) #return the leftmost/rightmost n characters of str respectively, if n<=> NULL then nothing is returned
select left('123',3), right('123456',3),left('123',NULL);


5. lpad(str,n,pad)/rpad(str,n,pad) #Use the string pad to fill the leftmost/rightmost of str, until the str contains n characters,
select name,lpad(name, 10,'#'),rpad(name,10,'@') from users;


6. trim(str)/ltrim(str)/rtrim(str) #Remove the left and right spaces/left spaces/right spaces of the string str
select concat('#',trim(" abc "),'#'),concat( '#',ltrim(' abc '),'#'),concat('#',rtrim(' abc '),'#');


7. replace(str,sear_str,sub_str) #Replace all occurrences of sear_str in the string str with sub_str
select replace('abcdefgabcd','cd','XXX');


8. strcmp(str1,str2) #Compare strings str1, str2 in ASCII code, return -1 (str1< str2)/0(str1= str2)/1(str1 > str2)
select strcmp('aa','bb '),strcmp('aa','aa'),strcmp('bb','aa');


9. substring(str,n,m) #Return a string of m characters from n in the string str
select substring('abcdef',2,3);
select name,substring(name,1,2) as subname from users;


2. Numerical function
1, abs(x) #return the absolute value of x
select abs(10), abs(-10);
select abs(age) from users;


2. ceil(x) #return the smallest integer greater than x
3, floor(x) #return the largest integer less than x
select ceil(2.1), ceil(2.5), ceil(2.9), floor(2.1), floor(2.5 ),floor(2.9);


4. mod(x,y) #Return the modulo of x/y, which is the same as x%y
select mod(null,11);


5. rand() #return a random number between 0 and 1
select rand();
select ceil(rand() * 100); #take an integer random number between 0 and 100
select floor(rand() * 100) ;


6. round(n,m) #Return the value of n with m decimal places after rounding, the m value defaults to 0
select round(1.23);
select round(1.456,2);


7. truncate(n,m) #Return the value of n truncated to m decimal places
select truncate(1.234,2);
select truncate(1.235,2),round(1.235,2);


3. Date function
1, curdate() #return the current date
2, curtime() #return the current time
select curdate(), curtime();


3. now() #return the current date + time
select now();


4. unix_timestamp(now())#Returns the timestamp of the current unix time
select unix_timestamp(now()); #The number of seconds from the first year of the computer (1971-1-100:00:00) to the present


5. from_unixtime() #Convert the timestamp (integer) to the form of "date + time (xx-xx-xxxx:xx:xx)"
select from_unixtime(1392853616);


6, week(now()) #Return the current time is the first week
7, year(now()) #Return the current is XX year
8, hour(now())/hour(curtime()) #Return the current time Hours
9, minute(curtime()) #Return the number of minutes in the current period
...
select week(now()), year(now()), hour(now());
select week(from_unixtime(1392853616)); #return the number of periods in the unix timestamp


10. month name(now())/monthname(curdate()) #Return the English name of the current month


11. date_format(now(),"%Y-%M-%D%H:%I%S") #Format the current time
select date_format(now(),"%Y-%m-%d %H :%i%s");
select date_format(now(),"%y%m%d %H:%i%s");
Fourth, flow control function
1, if(value,true,false) #if value If the value is true, return true, otherwise, return false
select if (salary > 3000,'Hight','Low') from salary;
select id,salary, if (salary <=> NULL,'NULL','NOT NULL ') from salary;


2. ifnull(value1,value2)#If value1 is not empty, return value1, otherwise return value2 #Can
be used for null replacement
select ifnull(salary,0.00) from salary;


3. case when [value] then ... else ...end #If the value is true, execute the statement after then, otherwise execute the statement after eles, don't forget to end!
select case when salary <= 3000 then "Low" else "Hight" end from salary;


Five, other functions
1, database() #current database
2, version() #current database version
3, user() #currently logged in user
select database();


4. inet_aton(ip) #network byte order of ip address
select inet_aton('192.168.139.1');


5. inet_ntoa() #return the ip represented by the number
select inet_ntoa(3232271105);


6. password(str) #Return the encrypted str string
select password("123456"); #Return a 41-bit long encrypted string, just used to encrypt MySQL system users
7. md5() #In the application Data encryption is performed in the C++ program, for example,
select md5("123456") in a C++ program;

 

http://blog.163.com/info_technology/blog/static/12781505420122755150278/

http://blog.csdn.net/zjf280441589/article/details/19547623

More functions:

http://www.mamicode.com/info-detail-250393.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327066529&siteId=291194637