sql 函数执行动态sql
SQL provides many built-in functions to perform operations on data. These functions are useful while performing mathematical calculations, string concatenations, sub-strings etc. SQL functions are divided into two categories,
SQL提供了许多内置函数来对数据执行操作。 这些函数在执行数学计算,字符串连接,子字符串等时很有用。SQL函数分为两类,
Aggregate Functions
汇总功能
Scalar Functions
标量函数
汇总功能 (Aggregate Functions)
These functions return a single value after performing calculations on a group of values. Following are some of the frequently used Aggregrate functions.
对一组值执行计算后,这些函数将返回单个值。 以下是一些常用的聚集函数。
AVG()
函数 (AVG()
Function)
Average returns average value after calculating it from values in a numeric column.
从数字列中的值计算平均值后,平均值返回平均值。
Its general syntax is,
它的一般语法是
SELECT AVG(column_name) FROM table_name
使用AVG()函数 (Using AVG() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 史考特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query to find average salary will be,
SQL查询找到平均薪水会,
SELECTavg(salary) from Emp;
Result of the above query will be,
以上查询的结果将是,
avg(salary) |
---|
8200 |
平均工资 |
---|
8200 |
COUNT()
函数 (COUNT()
Function)
Count returns the number of rows present in the table either based on some condition or without condition.
Count根据某种条件或无条件返回表中存在的行数。
Its general syntax is,
它的一般语法是
SELECT COUNT(column_name) FROM table-name
使用COUNT()函数 (Using COUNT() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 史考特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query to count employees, satisfying specified condition is,
通过SQL查询对员工进行计数,满足指定条件的是,
SELECT COUNT(name) FROM Emp WHERE salary = 8000;
Result of the above query will be,
以上查询的结果将是,
count(name) |
---|
2 |
计数(名称) |
---|
2 |
COUNT(与众不同)的示例 (Example of COUNT(distinct))
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 史考特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query is,
SQL查询是
SELECT COUNT(DISTINCT salary) FROM emp;
Result of the above query will be,
以上查询的结果将是,
count(distinct salary) |
---|
4 |
计数(薪水不同) |
---|
4 |
FIRST()
函数 (FIRST()
Function)
First function returns first value of a selected column
第一个函数返回所选列的第一个值
Syntax for FIRST function is,
FIRST函数的语法是
SELECT FIRST(column_name) FROM table-name;
使用FIRST()函数 (Using FIRST() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 史考特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query will be,
SQL查询将
SELECT FIRST(salary) FROM Emp;
and the result will be,
结果是
first(salary) |
---|
9000 |
第一(薪水) |
---|
9000 |
LAST()
函数 (LAST()
Function)
LAST function returns the return last value of the selected column.
LAST函数返回所选列的返回最后一个值。
Syntax of LAST function is,
LAST函数的语法是
SELECT LAST(column_name) FROM table-name;
使用LAST()函数 (Using LAST() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 史考特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query will be,
SQL查询将
SELECT LAST(salary) FROM emp;
Result of the above query will be,
以上查询的结果将是,
last(salary) |
---|
8000 |
最后(薪水) |
---|
8000 |
MAX()
函数 (MAX()
Function)
MAX function returns maximum value from selected column of the table.
MAX函数从表的选定列返回最大值。
Syntax of MAX function is,
MAX函数的语法是
SELECT MAX(column_name) from table-name;
使用MAX()函数 (Using MAX() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 史考特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query to find the Maximum salary will be,
SQL查询找到最高薪水会,
SELECT MAX(salary) FROM emp;
Result of the above query will be,
以上查询的结果将是,
MAX(salary) |
---|
10000 |
最高薪水 |
---|
10000 |
MIN()
函数 (MIN()
Function)
MIN function returns minimum value from a selected column of the table.
MIN函数从表的选定列中返回最小值。
Syntax for MIN function is,
MIN函数的语法是
SELECT MIN(column_name) from table-name;
使用MIN()函数 (Using MIN() function)
Consider the following Emp table,
考虑以下Emp表,
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 史考特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query to find minimum salary is,
SQL查询找到的最低工资是
SELECT MIN(salary) FROM emp;
Result will be,
结果将是
MIN(salary) |
---|
6000 |
最低工资 |
---|
6000 |
SUM()
函数 (SUM()
Function)
SUM function returns total sum of a selected columns numeric values.
SUM函数返回选定列数值的总和。
Syntax for SUM is,
SUM的语法是
SELECT SUM(column_name) from table-name;
使用SUM()函数 (Using SUM() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | Scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 史考特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query to find sum of salaries will be,
SQL查询查找工资总额,
SELECT SUM(salary) FROM emp;
Result of above query is,
以上查询的结果是,
SUM(salary) |
---|
41000 |
总和(薪水) |
---|
41000 |
标量函数 (Scalar Functions)
Scalar functions return a single value from an input value. Following are some frequently used Scalar Functions in SQL.
标量函数从输入值返回单个值。 以下是一些SQL中常用的标量函数。
UCASE()
函数 (UCASE()
Function)
UCASE function is used to convert value of string column to Uppercase characters.
UCASE函数用于将字符串列的值转换为大写字符。
Syntax of UCASE,
UCASE的语法 ,
SELECT UCASE(column_name) from table-name;
使用UCASE()函数 (Using UCASE() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | anu | 22 | 9000 |
402 | shane | 29 | 8000 |
403 | rohan | 34 | 6000 |
404 | scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 沙恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 斯科特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query for using UCASE is,
使用UCASESQL查询是,
SELECT UCASE(name) FROM emp;
Result is,
结果是
UCASE(name) |
---|
ANU |
SHANE |
ROHAN |
SCOTT |
TIGER |
UCASE(名称) |
---|
澳大利亚国立大学 |
尚 |
罗汉 |
斯科特 |
虎 |
LCASE()
函数 (LCASE()
Function)
LCASE function is used to convert value of string columns to Lowecase characters.
LCASE函数用于将字符串列的值转换为Lowecase字符。
Syntax for LCASE is,
LCASE的语法是
SELECT LCASE(column_name) FROM table-name;
使用LCASE()函数 (Using LCASE() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | Anu | 22 | 9000 |
402 | Shane | 29 | 8000 |
403 | Rohan | 34 | 6000 |
404 | SCOTT | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 谢恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 斯科特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query for converting string value to Lower case is,
用于将字符串值转换为小写SQL查询是,
SELECT LCASE(name) FROM emp;
Result will be,
结果将是
LCASE(name) |
---|
anu |
shane |
rohan |
scott |
tiger |
LCASE(名称) |
---|
阿努 |
沙恩 |
罗汉 |
斯科特 |
虎 |
MID()
函数 (MID()
Function)
MID function is used to extract substrings from column values of string type in a table.
MID函数用于从表中字符串类型的列值中提取子字符串。
Syntax for MID function is,
MID函数的语法是
SELECT MID(column_name, start, length) from table-name;
使用MID()函数 (Using MID() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | anu | 22 | 9000 |
402 | shane | 29 | 8000 |
403 | rohan | 34 | 6000 |
404 | scott | 44 | 10000 |
405 | Tiger | 35 | 8000 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000 |
402 | 沙恩 | 29 | 8000 |
403 | 罗汉 | 34 | 6000 |
404 | 斯科特 | 44 | 10000 |
405 | 虎 | 35 | 8000 |
SQL query will be,
SQL查询将
SELECT MID(name,2,2) FROM emp;
Result will come out to be,
结果会是
MID(name,2,2) |
---|
nu |
ha |
oh |
co |
ig |
MID(名称,2,2) |
---|
怒 |
哈 |
哦 |
合作 |
ig |
ROUND()
函数 (ROUND()
Function)
ROUND function is used to round a numeric field to number of nearest integer. It is used on Decimal point values.
ROUND函数用于将数字字段舍入到最接近的整数。 用于小数点值。
Syntax of Round function is,
Round函数的语法是
SELECT ROUND(column_name, decimals) from table-name;
使用ROUND()函数 (Using ROUND() function)
Consider the following Emp table
考虑下面的Emp表
eid | name | age | salary |
---|---|---|---|
401 | anu | 22 | 9000.67 |
402 | shane | 29 | 8000.98 |
403 | rohan | 34 | 6000.45 |
404 | scott | 44 | 10000 |
405 | Tiger | 35 | 8000.01 |
开斋节 | 名称 | 年龄 | 薪水 |
---|---|---|---|
401 | 阿努 | 22 | 9000.67 |
402 | 沙恩 | 29 | 8000.98 |
403 | 罗汉 | 34 | 6000.45 |
404 | 斯科特 | 44 | 10000 |
405 | 虎 | 35 | 8000.01 |
SQL query is,
SQL查询是
SELECT ROUND(salary) from emp;
Result will be,
结果将是
ROUND(salary) |
---|
9001 |
8001 |
6000 |
10000 |
8000 |
回合(工资) |
---|
9001 |
8001 |
6000 |
10000 |
8000 |
sql 函数执行动态sql