mysql函数示例_mysql函数备忘单和示例

mysql函数示例

There are a ton of functions provided by MySQL and most of them are used extensively. I will be providing the most commonly used functions with a short description. The intension of the article is to provide one spot for all MySQL functions so that one can quickly go through it before your interview or an examination. I’m assuming you already have basic knowledge of SQL. Without wasting your time let me directly jump into the functions.

MySQL提供了很多功能,其中大多数功能得到了广泛使用。 我将以简短的说明提供最常用的功能。 本文的目的是为所有MySQL函数提供一个位置,以便您可以在面试或考试之前快速通过它。 我假设您已经具备SQL的基本知识。 在不浪费您时间的情况下,让我直接进入功能。

Before that, I would like you to know that I have used MySQL Workbench to execute the queries and employee database. Let’s quickly have a look at the employee dataset description.

在此之前,我想让您知道我已经使用MySQL Workbench执行查询和员工数据库。 让我们快速看一下员工数据集描述。

mysql函数示例_mysql函数备忘单和示例_第1张图片
  1. EMPNO: Employee ID

    EMPNO:员工编号
  2. ENAME: Employee name

    ENAME:员工姓名
  3. JOB: Designation

    职位:职务
  4. MGR: Manager ID

    MGR:经理ID
  5. HIREDATE: Date when the employee was hired

    雇用日期:雇用员工的日期
  6. SAL: Salary per month

    SAL:每月工资
  7. COMM: Commission earned

    COMM:赚取佣金
  8. DEPTNO: Department number the employee belongs to

    DEPTNO:员工所属的部门编号

Also, upcoming queries would be more clear if you know the values in the employee table.

另外,如果您知道employee表中的值,则即将到来的查询将更加清晰。

SELECT * FROM emps;
mysql函数示例_mysql函数备忘单和示例_第2张图片

长度( ) (LENGTH( ))

CHAR_LENGTH, CHARACTER_LENGTH, and LENGTH, all three functions give the same result which is the number of characters in the given string. All three take one parameter which is a string value.

CHAR_LENGTH,CHARACTER_LENGTH和LENGTH,这三个函数都给出相同的结果,即给定字符串中的字符数。 这三个参数都采用一个参数,它是一个字符串值。

SELECT ename, CHAR_LENGTH(ename), CHARACTER_LENGTH(ename), LENGTH(ename) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第3张图片

CONCAT () (CONCAT( ))

The concatenation of string is a very commonly used technique in all programming languages. SQL provides it too. CONCAT is used to join two or more values into a single string value, it can join any type of data.

在所有编程语言中,字符串的连接是一种非常常用的技术。 SQL也提供了它。 CONCAT用于将两个或多个值连接为单个字符串值,它可以连接任何类型的数据。

SELECT CONCAT(ename, ' works as ',job) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第4张图片

格式() (FORMAT( ))

It formats the floating-point number to specified decimal places and returns the value as a string.

它将浮点数格式化为指定的小数位,然后将值作为字符串返回。

Parameters

参量

number: Required. The number to be formatted

编号:必填。 要格式化的数字

decimal_places: Required. The number of decimal places for number. If this parameter is 0, this function returns a string with no decimal places

小数位数:必需。 数字的小数位数 如果此参数为0,则此函数返回不带小数位的字符串

SELECT comm, FORMAT(comm, 1) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第5张图片

插入() (INSERT( ))

Used to insert a string in place of another string starting from a specified position until a certain number of characters.

用于从一个指定位置开始插入一个字符串代替另一个字符串,直到一定数量的字符。

In this example, we are replacing all JOB values to ‘company employee’ starting from position 1 of JOB value taking all the characters of it(length).

在此示例中,我们将所有JOB值从JOB值的位置1开始替换为“公司雇员”,并取其所有字符(长度)。

Parameters

参量

string: Required. The string that will be modified

字符串:必填。 将被修改的字符串

position: Required. The position where to insert string2

职位:必填。 插入string2的位置

number: Required. The number of characters to replace

编号:必填。 要替换的字符数

string2: Required. The string to insert into the string

string2:必填。 要插入字符串的字符串

SELECT ename, job, INSERT(JOB,1, length(job), 'company employee') FROM emps;
mysql函数示例_mysql函数备忘单和示例_第6张图片

INSTR () (INSTR( ))

Returns position of the 1st occurrence of the string in another string. Here, ‘g’ 1st occurs at position 19 in the string ‘ Medium is best blogging platform’.

返回该字符串在另一个字符串中第一次出现的位置。 在这里,“G”第一出现在字符串“媒介是最好的BLO更改平台”在位置19。

SELECT INSTR('Medium is best blogging platform', "g") AS MatchPosition;OUTPUT: 19

找到() (LOCATE( ))

Locate is the improved version of INSTR which addresses the drawback of INSTR. What if we want the position of the third occurrence of the string? LOCATE gives us the flexibility to specify from what position to start the search from. Below, we start searching from position 21 of the string ‘Medium is best blogging platform’ to get the position of the third occurrence of ‘g’.

查找是INSTR的改进版本,解决了INSTR的缺点。 如果我们想要字符串第三次出现的位置怎么办? LOCATE使我们可以灵活地指定从哪个位置开始搜索。 下面,我们从字符串“ Medium is best bloggin g platform”的位置21开始搜索,以获得第三次出现的“ g”的位置。

SELECT LOCATE("g", "Medium is best blogging platform", 21) AS MatchPosition;OUTPUT: 23

UCASE () ,LCASE () (UCASE( ), LCASE( ))

Very straight forward, UCASE to convert string to uppercase and LCASE to convert a string into lowercase.

非常简单,UCASE将字符串转换为大写,而LCASE将字符串转换为小写。

SELECT job, LCASE(job), UCASE(job) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第7张图片

() ,右() (LEFT( ), RIGHT( ))

Left: Extract the specified number of characters from the beginning of the string.

左:从字符串的开头提取指定数量的字符。

Right: Extract the specified number of characters from the end of the string.

右:从字符串末尾提取指定数量的字符。

Below, we are extracting one character from the beginning and end of each string.

下面,我们从每个字符串的开头和结尾提取一个字符。

SELECT job, LEFT(job, 1), RIGHT(job, 1) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第8张图片

更换() (REPLACE( ))

Replaces all the occurrences of the specified string with another specified string. We are replacing all the uppercase ‘A’ with lowercase ‘a’ in each job value. I have replaced a single character with another single character but the same can be done with string. Go ahead and experiment, change ‘man’ with ‘women’.

将所有出现的指定字符串替换为另一个指定字符串。 我们在每个作业值中将所有大写字母“ A”替换为小写字母“ a”。 我已经用另一个字符替换了一个字符,但是使用字符串也可以做到这一点。 继续尝试,用“女性”来改变“男人”。

SELECT job, REPLACE(job, 'A', 'a') from emps;
mysql函数示例_mysql函数备忘单和示例_第9张图片

SUBSTR() (SUBSTR( ))

To extract a substring from a string, we have to specify starting positions and the number of characters needed from the start point. Here, we are extracting the first three characters of each job value. That is, 3 characters starting from position 1 of the string.

要从字符串中提取子字符串,我们必须指定起始位置以及从起始点开始需要的字符数。 在这里,我们提取每个作业值的前三个字符。 即,从字符串的位置1开始的3个字符。

SELECT job, SUBSTR(job, 1, 3) AS Abbrevation FROM emps;
mysql函数示例_mysql函数备忘单和示例_第10张图片

汇总功能 (Aggregate Functions)

Aggregate functions provided by MySQL are max, min, avg, and count. I have demonstrated each by finding maximum, minimum, mean/average, and the total count of salaries.

MySQL提供的聚合函数是max,min,avg和count。 我已经通过找到最大,最小,均值/平均数以及工资总数来证明了每个方法。

SELECT MAX(sal), MIN(sal), AVG(sal), COUNT(sal), SUM(SAL) FROM emps;
MySQL functions

楼(),楼() (FLOOR( ), CEIL( ))

Irrespective of the decimal value, the floor returns the nearest integer less than or equal to the float number, and ceil returns the nearest integer greater than or equal to the float number.

不论十进制值如何,下限都返回小于或等于浮点数的最接近整数,而ceil返回大于或等于浮点数的最接近整数。

mysql函数示例_mysql函数备忘单和示例_第11张图片
mathsisfun.com mathsisfun.com
SELECT comm, FLOOR(comm), CEIL(COMM) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第12张图片

电源() (POWER( ))

Returns the value of the number raised to another specified number. In this case, it returns the square of all the salaries.

返回增加到另一个指定数字的数字的值。 在这种情况下,它将返回所有薪水的平方。

SELECT ename, sal, pow(sal,2) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第13张图片

圆() (ROUND( ))

Rounds the number to a specified number of decimal places. It takes two parameters, the number to be rounded and the required decimal places.

将数字四舍五入到指定的小数位数。 它有两个参数,要取整的数字和所需的小数位。

mysql函数示例_mysql函数备忘单和示例_第14张图片
mathsisfun.com mathsisfun.com

Commission is rounded to 1, 2, and 3 decimal places respectively.

佣金分别舍入到1、2和3个小数位。

SELECT comm, ROUND(comm,1), ROUND(comm,2), ROUND(comm,3) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第15张图片

TRUNCATE() (TRUNCATE( ))

Returns the value truncated to a specified number of decimal values. If the second argument is 0 then the decimal point is removed, if positive then the specified number of values in the decimal part is truncated, if negative then the specified number of values in the integer part is truncated.

返回截断为指定数量的十进制值的值。 如果第二个参数为0,则删除小数点;如果为正数,则舍去小数部分中指定数量的值;如果为负数,则舍去整数部分中的指定数量。

mysql函数示例_mysql函数备忘单和示例_第16张图片
lideshare.net lideshare.net
SELECT comm, TRUNCATE(comm,1), TRUNCATE(comm,-1) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第17张图片

DIfference between round and truncate:

舍入和截断之间的区别:

Round, rounds the value to the nearest integer while truncate just drops the extra value.

四舍五入,将值四舍五入到最接近的整数,而截断仅丢弃多余的值。

ADDDATE() (ADDDATE( ))

Used to add a time/date interval to date and then return the date. The adding unit can be of type day, month, year, quarter, etc. The list is as below.

用于向日期添加时间/日期间隔,然后返回日期。 添加单位可以是日,月,年,季度等类型。列表如下。

mysql函数示例_mysql函数备忘单和示例_第18张图片
SELECT hiredate, ADDDATE(hiredate, INTERVAL 10 DAY), ADDDATE(hiredate, INTERVAL 2 MONTH), ADDDATE(hiredate, INTERVAL 2 YEAR) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第19张图片

CURDATE(),CURTIME(),CURRENT_TIMESTAMP() (CURDATE( ), CURTIME( ), CURRENT_TIMESTAMP( ))

This is very simple, returns the current date, current time, and current date and time together known as timestamp.

这非常简单,将当前日期,当前时间以及当前日期和时间一起返回,称为时间戳。

SELECT curdate(), CURTIME(), current_timestamp();
MySQL functions

DATEDIFF() (DATEDIFF( ))

Suppose if we want to display the number of experiences in years an employee has in the company, we need to subtract the current date with the date of hire. This is where DATEDIFF() comes handy, it returns the number of days between two dates.

假设如果要显示员工在公司中所经历的年数,则需要将当前日期减去雇用日期。 这是DATEDIFF()派上用场的地方,它返回两个日期之间的天数。

SELECT ename, hiredate, DATEDIFF(CURDATE(), hiredate) as 'experience in days' FROM emps;
mysql函数示例_mysql函数备忘单和示例_第20张图片

to get the difference in years we need to do some math explicitly: divide by 365, and round the resultant value.

为了获得年的差值,我们需要明确地做一些数学运算:除以365,然后将结果值取整。

SELECT ename, hiredate, ROUND(DATEDIFF(CURDATE(), hiredate)/365) as 'experience in years' FROM emps;
mysql函数示例_mysql函数备忘单和示例_第21张图片

DAYNAME(),DAYOFMONTH(),DAYOFWEEK(),DAYOFYEAR() (DAYNAME( ), DAYOFMONTH( ), DAYOFWEEK( ), DAYOFYEAR( ))

The DAYNAME function returns the name of the day (Sunday, Monday, Tuesday, etc.) given a date value.

DAYNAME函数返回给定日期值的日期名称(星期日,星期一,星期二等)。

The DAYOFMONTH returns the number of days since the beginning of the year given a date value.

DAYOFMONTH返回给定日期值的自年初以来的天数。

The DAYOFWEEK basically returns an integer representing the day of the week starting from Sunday as 0 given the date value. Look at DAYNAME in the below table, Wednesday is the 4th(DAYOFWEEK) day of the week, Friday is the 6th(DAYOFWEEK) day of the week, and so on.

DAYOFWEEK基本上返回一个整数,该整数表示给定日期值,从周日开始的星期几为0。 查看下表中的DAYNAME,星期三是一周的第四天(DAYOFWEEK),星期五是一周的第六天(DAYOFWEEK),依此类推。

The DAYOFYEAR returns an integer representing the day count since the beginning of the year(January 1st). Below, 17th December 1980 is the 352nd day of the year 1980 from January 1.

DAYOFYEAR返回一个整数,表示自年初(1月1日)以来的天数。 下方的1980年12月17日是1980年12月1日至1月1日的第352天。

SELECT DAYNAME(hiredate), DAYOFMONTH(hiredate), DAYOFWEEK(hiredate), DAYOFYEAR(hiredate) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第22张图片

提取() (EXTRACT( ))

Used to extract the specified part of the given date.

用于提取给定日期的指定部分。

SELECT EXTRACT(MONTH FROM hiredate), EXTRACT(YEAR FROM hiredate), EXTRACT(DAY FROM hiredate) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第23张图片

We can extract any of the given below part of the information from date.

我们可以从日期开始提取以下信息中的任何给定部分。

mysql函数示例_mysql函数备忘单和示例_第24张图片

25美分硬币( ) (QUARTER( ))

Returns the quarter of the year in which the given date falls in.

返回给定日期所在的一年的四分之一。

  • January-March falls in the 1st quarter.

    一月至三月属于第一季度。
  • April-June falls in the 2nd quarter.

    4-6月属于第二季度。
  • July-September falls in the 3rd quarter.

    7月至9月为第3季度。
  • October-December falls in the 4th quarter.

    10月至12月为第4季度。
SELECT hiredate, QUARTER(hiredate) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第25张图片

IF() (IF( ))

Returns value if the given condition is true else another value.

如果给定条件为true,则返回值,否则返回另一个值。

IF(condition, value_if_true, value_if_false)

IF(条件value_if_truevalue_if_false )

SELECT IF(SAL<1000, "YES", "NO") FROM EMPS;
mysql函数示例_mysql函数备忘单和示例_第26张图片

案件( ) (CASE( ))

Suppose we would like to categorize employees based on their salary. Salary less than 1000 as Underpaid, between 1000 and 3000 as Fairly paid, and more than 3000 as Overpaid. We have to use the nested if function as below.

假设我们要根据员工的薪水进行分类。 工资少于1000的工资不足,合理工资在1000到3000之间,工资超过3000的工资超过正常工资。 我们必须使用嵌套的if函数,如下所示。

SELECT ename, sal, IF(sal<1000, “Underpaid”, IF(sal>1000 AND sal>3000,’Fairly paid’,’Overpaid’)) FROM EMPS;

This is fine if there are only a few conditions, what if we have several conditions? then we need to use the CASE function as below.

如果只有几个条件,这很好,如果我们有几个条件,该怎么办? 那么我们需要使用CASE函数,如下所示。

SELECT ename, sal,
CASE
WHEN sal<1000 THEN 'Underpaid'
WHEN sal>1000 AND sal<3000 THEN 'Fairly paid'
ELSE 'Overpaid'
END AS 'salary status'
FROM emps;
mysql函数示例_mysql函数备忘单和示例_第27张图片

COALESCE() (COALESCE( ))

COALESCE takes a list of arguments and returns the first non-null value. In the below example, if the value of comm is null then it returns zero.

COALESCE接受参数列表并返回第一个非空值。 在下面的示例中,如果comm的值为null,则它返回零。

SELECT ename, comm, COALESCE(comm, 0) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第28张图片

数据库() (DATABASE( ))

It returns the name of the current database you are working in.

它返回您正在使用的当前数据库的名称。

SELECT DATABASE();

一片空白( ) (ISNULL( ))

Returns 0 if the given value of a non-null else returns 1.

如果给定的非null值返回1,则返回0。

SELECT comm, ISNULL(comm) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第29张图片

NULLIF() (NULLIF( ))

It takes two arguments and returns null if both the values are the same else the first argument passed. Arguments can be of any type.

它接受两个参数,如果两个值相同,则返回null,否则将传递第一个参数。 参数可以是任何类型。

SELECT NULLIF(25, 25);
MySQL functions

Below we are comparing if salary and commission of each employee are the same. We can see no employee has the same salary and commission hence returns salary since it is the first argument passed.

下面我们比较每个员工的工资和佣金是否相同。 我们看到没有雇员拥有相同的薪水和佣金,因此,因为这是传递的第一个参数,因此会返回薪水。

SELECT sal, comm, NULLIF(sal, comm) FROM emps;
mysql函数示例_mysql函数备忘单和示例_第30张图片

Hope this was helpful and thank you for reading.

希望这对您有所帮助,并感谢您的阅读。

mysql函数示例_mysql函数备忘单和示例_第31张图片
Photo by Pete Pedroza on Unsplash Pete Pedroza在 Unsplash上 拍摄的照片

翻译自: https://medium.com/analytics-vidhya/mysql-functions-cheatsheet-with-examples-3a08bb36d074

mysql函数示例

你可能感兴趣的:(mysql,python,leetcode,sql)