The use of Mysql common functions [Summary]

Use select to test functions directly: For example, select 'concat';
Insert picture description here
here are some commonly used functions in Mysql:

1. concat()Function: splice the parameters inside concat together

  • Example: Query the employee's name and salary, the required salary unit is yuan:
    select ename,concat(sal,'元') 工资 from emp;

2. Numerical calculation: + - * / %(mod(7,2) is equivalent to 7%2)

  • Example: Query the unit price, inventory and total value of each product in the product table (unit price * inventory)
    select price,num,price * num '总价值' from t_item;
  • Example: Query the name, salary, and year-end bonus of each employee in the employee table (five months' salary)
    select ename,sal,sal * 5 '年终奖' from emp;

3. Date related functions:

  1. Get year, month, day, hour, minute and second:select now()
  2. Get the current date:select curdate()
  3. Get the hour, minute and second of the current time:select curtime()
  4. Extract the year, month, and day from the year, month, day, hour, minute, and second:select date(now())
  5. Extract hour, minute, and second from year, month, day, hour, minute, and second:select time(now())
  6. Extract time components from year, month, day, hour, minute, and second (year, month, day, hour, minute, and second):extract()
    • Year of extraction:select extract(year from now());
    • Extract month:select extract(month from now());
    • Withdrawal date:select extract(day from now());
    • When extracting:select extract(hour from now());
    • Extract points:select extract(minute from now());
    • Extract seconds:select extract(second from now());
  • Example: Query the names and entry years of all employees in the employee table:
    select ename,extract(year from hiredate) from emp;

4. Date formatting:date_format(时间,格式)

  • %Y: Four years %y,: Two years

  • %m: Two months %c,: one month

  • %d:day,

  • %H: 24-hour system,: %h12-hour system

  • %i:Minute

  • %s:second

  • Example: Convert the current time to format:select date_format(now(),'%Y年%m月%d日%H时%i分%s秒');
    Insert picture description here

  • Example: query product name and product creation date (format: x year x month x day)
    select title,date_format(date(curdate_time),'%Y年%m月%d日') from t_item;
    select title,date_format(curdate_time,'%Y年%m月%d日') from t_item;

5. Convert a non-standard date string into a standard time format:str_to_date(时间字符串,字符串格式)

  • Example: 14.08.2018 08:00:00:
    select str_to_date('14.08.2018 08:00:00','%m.%d.%Y %H:%i:%s');

6. ifnull(x,y)Function:

Explanation: age=ifnull(x,18) if the value of x is null then age=18, if not null then age=x;

  • Example: Modify the value of the bonus as null in the employee table to 0:
    update emp set comm = ifnull(comm,0);

7. Aggregate function: used for statistics of multiple data

  1. Summation:sum(字段名)
    • Example: Query the total salary of department 10 in the emp table:
      select sum(sal) from emp where deptno = 10;
  2. average value:avg(字段名)
    • Example: Query the average wage of all employees in the emp table:
      select avg(sal) from emp;
  3. Maximum value:max(字段名)
    • Example: Query the highest bonus of employees in department 30:
      select max(comm) from emp where deptno = 30;
  4. Minimum:min(字段名)
    • Example: Query the unit price of the cheapest product in the product table:
      select min(price) from t_item;
  5. Statistics: used in count(数量名)generalcount(*)
    • Example: Count how many people are in department 30:
      select count(*) from emp where deptno = 30;

8. Functions related to strings: (Small labels start from 1, generally including the head but not the tail)

  1. Get string length:char_length(str)

    • Example: Get the names of all employees and the length of their name strings:
      select ename,char_length(ename) from emp;
  2. Get the position of a string in another string:instr(str,substr)

    • select instr('adegc','e');
      Insert picture description here
  3. Insert string:insert(str,start,length,newStr);

    • select insert('abcdefg',2,4,'m');
      Insert picture description here
  4. To uppercase:, To upper(字符串)lowercase:lower(字符串)

    • select upper('nba'),lower('NBA');
      Insert picture description here
  5. Intercept on the left:, left(str,length)Intercept on the right:right(str,length)

    • select left('abcdefg',2),right('abcdefg',2);
      Insert picture description here
  6. Remove the blanks at both ends: trim(str)(The middle can't be removed)

    • select trim(' a b ');
      Insert picture description here
  7. Intercept string: substring(str,截取开始的位置,截取的长度)(including head but not tail)

    • select substring('abcdefg',3,2);
      Insert picture description here
  8. Repetition: repeat(str,count)count is the number of repetitions

    • select repeat('ab',2);
      Insert picture description here
  9. replace:replace(str,old,new)
    select replace('This is mysql','my','your');
    Insert picture description here

  10. Reverse:reverse(str)

    • select reverse('abc');
      Insert picture description here

9. Mathematical related functions:

  1. Round down:floor(num)
    • select floor(3.48);
      Insert picture description here
  2. Rounding:round(num) , round(num,m)m represents the number of decimal places
    • select round(3.84);
      Insert picture description here
    • select round(3.8456,3);
      Insert picture description here
  3. Non-rounding (retain a few decimal places):truncate(num,m)
    • select truncate(3.8456,3);
      Insert picture description here
  4. Get random number: rand() (random number with value range 0-1)
    • Example: Find a random number from 5-10:
      select floor(rand()*6)+5;
      Insert picture description here

    • Example: Find a random number from 3-8:
      select floor(rand()*6)+3;
      Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108580218