msyql commonly used functions

In business development, mysql fact, there are many common functions, to facilitate query data.

Sometimes based on business needs can also be written to the memory process, but this is not recommended, because once business complicated sql storage process will be longer and longer, poor readability, the latter will find it harder to maintain.

Well, to start, said the main topic.

String related functions:

concat (s1, s2, s3, ...) - the connection parameters passed into a string.
   // For example, select concat ( 'aaa', 'bbb', 'ccc'), concat ( 'aaa', null); NULL string result of any connection will be NULL 
the INSERT (STR, X, Y, instr) - the string str from the start position x, y characters long substring is replaced with a string instr.
   // For example, SELECT INSERT ( 'beijing2008you', 12,3, 'Me') 
the LOWER (STR) and UPPER (str) - the string into lowercase or uppercase
    // e.g., select lower ( 'BEIJING') ; Upper SELECT ( 'Beijing'); 
the lEFT (STR, x), and rIGHT (str, x) - return the character x x leftmost and rightmost character string.
   // For example, select left ( 'qwertasd', 1); select right ( 'qwertasd', 1); if the second parameter is NULL, it will not return any string. 
LPAD (str, n, pad) and RPAD (str, n, pad) - of the leftmost and rightmost str be filled with a string pad,
   Until length n characters in length. //For example, SELECT LPAD ( '2008', 20 is, 'Beijing'); SELECT RPAD ( '2008', 20 is, 'Beijing') 
LTRIM (str) and RTRIM (str) - removing the left and right spaces string str .
   // For example, SELECT LTRIM ( '| Beijing'); SELECT RTRIM ( 'Beijing |'); 
LTRIM (str) and RTRIM (str) - removing the string str left and right spaces.
   // For example, SELECT LTRIM ( '| Beijing'); SELECT RTRIM ( 'Beijing |'); 
the REPEAT (str, x) - returns the result of repeated x times str.
   // For example, SELECT REPEAT ( 'MySQL',. 3) 
the REPLACE (str, a, B) - replace all occurrences of the string str string a string b.
   // For example, SELECT Replace ( 'beijing_2012', '_ 2012', '2008'); 
STRCMP (S1, s2) - the size of the ASCII value of a string compare s1 and s2. If s1 is smaller than s2, then Returns - . 1 ; if s1 and s2 are equal, then return 0 ; if s1 is greater than s2, returning1 .
   // ; e.g., SELECT strcmp ( 'A', 'B'), strcmp ( 'B', 'B'), strcmp ( 'C', 'B') 
- the TRIM (STR) at the beginning of the target string is removed and trailing spaces.
   // For example, SELECT TRIM ( 'Beijing2008 $ $'); 
the SUBSTRING (str, x, y) - returns a string of characters from a length y from the x-th string str position.
   // For example, select substring ( 'beijing2008', 8,4), substring ( 'beijing2008', 1,7);

Array function:

ABS (x) - returns the absolute value of x.
   // For example, SELECT ABS (-0.8); 
CEIL (x) - returns the greatest integer greater than x.
   // For example, SELECT ceil (-0.4); SELECT ceil (0.4); 
the FLOOR (x) - Usage returns the largest integer less than x, and CEIL just the opposite.
   // For example, SELECT Floor (0.4); 
the MOD (X, Y) - Returns the x / y of the mold. X% and the output y of the same modulus and modulus of any one of the result are NULL NULL.
   // For example, the MOD SELECT (15,10), the MOD (1,11), the MOD (NULL, 10); 
the RAND () - returns 0 to . 1 random value within. Each time the results are not the same.
   // For example, SELECT the RAND (), the RAND ();
    // e.g., SELECT ceil (100 * RAND ()), ceil (100 * RAND ()); 
the ROUND (x, Y) - Returns the rounding parameter x there decimal value of y bits. If is an integer, it will retain the number of bits y 0; if you do not write y, y is 0 by default, after coming x rounded to the nearest integer.Suitable for all digital decimal places keep the same situation.   
  // For example, SELECT the ROUND (1.1), the ROUND (1.136,2), the ROUND (1,2); TRUNCATE (x, y) - Returns the number x y bit result is truncated to decimal. // For example, select ROUND (1.235,2), TRUNCATE (1.235,2);

The date and time function:

CURDATE () - Returns the current date, containing only the date.
   // For example, SELECT CURDATE (); 
CURTIME () - returns the current time, minutes and seconds containing only.
   // For example, the SELECT CURTIME (); 
the NOW () - Returns the current date and time, year, month, day, hour all included.
   // For example, SELECT the NOW (); 
UNIX_TIMESTAMP (date) - Returns the date stamp date of UNIX.
   // For example, SELECT UNIX_TIMESTAMP (now ()); 
FROM_UNIXTIME (unixtime) - date time stamp value returned UNIXTIME.
   // For example, the SELECT FROM_UNIXTIME (1184134516); 
WEEK (DATE) and YEAR (DATE) - The former returns the given date is the first few weeks of the year, the latter returned to the date which is one year.   
  // For example, SELECT the WEEK (now ()), YEAR (now ()); HOUR (Time) and MINUTE (time) - The former returns to the hour of the time, which returns to the minutes of time. //For example, the SELECT HOUR (CURTIME ()), MINUTE (CURTIME ()); MONTHNAME (date) - returns the date of the English name of the month. // For example, SELECT MONTHNAME (now ()); the DATE_FORMAT (date, fmt) - Press string fmt formatting date date value. // For example, the DATE_FORMAT SELECT (now (), '% M,% D,% the Y'); DATE_ADD (date, INTERVAL expr type) - returns the phase difference to date date date INTERVAL period. // For example, SELECT now () Current, DATE_ADD (now (), Day 31 is the INTERVAL) after31days, DATE_ADD (now (), the INTERVAL ' 1_2 ' year_month) after_oneyear_twomonth; the DATEDIFF (date1, DATE2) - used to calculate the two dates the difference between the number of days. // For example, select DATEDIFF ( '2008-08-08', now ());

Process function:

IF (value, tf) - If the value is true, return t; otherwise f.
   // For example, SELECT IF (the salary> 2000, 'High', 'Low') from the salary; 
the IFNULL (value1, value2) - If value1 is not empty return value1, otherwise value2.
   // For example, SELECT IFNULL (the salary, 0) from the salary; 
the CASE the WHEN [value1] THEN [RESULT1] ... the ELSE [ default ] the END - If value1 is true, RESULT1 returned, otherwise default .   
  // For example, the salary When SELECT Case <= 2000 the then 'Low' the else 'High' End from the salary; the CASE [expr] the WHEN [VALUE1] THEN [result1] ... the ELSE [ default ] the END - If expr equal value1, returns result1 otherwise default .   // For example, select case salary when 1000 then ' low' when 2000 then 'mid'
salary;

Other functions:

DATABASE () - Returns the current database name. 
VERSION () - Returns the current database version. 
The USER () - Returns the currently logged on user name. 
INET_ATON (IP) - return the IP address of the network byte order represented. 
INET_NTOA (NUM) - returns the network byte order representative of an IP address. 
PASSWORD (str) - Returns the string str encrypted version, a 41 is the bit length of the string. (Only for the password) 
MD5 (str) - Returns the string str MD5 value, used to encrypt the data application.

 

 

  

Guess you like

Origin www.cnblogs.com/two-bees/p/11807221.html