Commonly used functions in sql statement (beginner sql)

I have summarized some commonly used functions myself and made a daily record. I hope that the improper place will point out!

Function method in sql statement:
1: Useful Aggregate function:
  • AVG ()-returns the average
  • COUNT ()-returns the number of rows
  • FIRST ()-returns the value of the first record
  • LAST ()-returns the value of the last record
  • MAX ()-returns the maximum value
  • MIN ()-returns the minimum value
  • SUM ()-returns the sum
2: Useful Scalar functions:
  • UCASE ()-Convert a field to uppercase
  • LCASE ()-convert a field to lower case
  • MID ()-extract characters from a text field, used in MySql
  • SubString (field, 1, end)-extract characters from a text field
  • LEN ()-returns the length of a text field
  • ROUND ()-rounds a number field to a specified number of decimal places
  • NOW ()-returns the current system date and time
  • FORMAT ()-format how a field is displayed
1: Useful Aggregate function:
The following is an example of the above function:
AVG: Syntax select AVG (column_name) from table_name; returns the number of a record. average value
Filter out all records greater than or equal to the average of a field:
示例:select column1,column2 from table_name where column1 >=(select AVG(column1) from table_name);

COUNT (): Syntax
COUNT (column_name): The function returns the number of values ​​of the specified column (NULL is not counted).
示例:select COUNT(column_name) from table_name;
COUNT (*): Returns the number of all records in the table that meet the conditions.
The COUNT (distinct column_name) function returns the number of different values ​​of the specified column (deduplication).

FIRST(column_name): Last (column_name) Replace ASC with DESC
Returns the first record of this field
语法:select FIRST(column_name) from table_name;
Equivalent to, mysql database: select column_name from table_name limit 1;
sql server : select top 1 column_name from table_name order by column_name ASC;
Oracle :select column_name from table_name order by column_name ASC where rownum <= 1;

MAX (column_name) maximum MIN (column_name) minimum
语法:select MAX( column_name)from table_name;

SUM (column_name) returns the sum of all record values ​​in this field
sum () finds the accumulation of the value of each row of the field count () finds the accumulation of the number of rows
Sum () for null, do not calculate count () that there is no null (do not calculate).
语法: select SUM(column_name) from table_name;
group to group by column_name a field for binding the aggregate function, the column of the result set are grouped according to one or more.
语法: select column_name,函数(column_name) from table_name where 条件 group by column_name;
The HAVING clause is used to process data after group by.
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。HAVING 子句可以让我们筛选分组后的各组数据。
语法:select column_name,函数(column_name) from table_name where 条件 group by column_name HAVING 函数(column_name)operator value;
2: 有用的 Scalar 函数:
UCASE(column_name) UCASE() 函数把字段的值转换为大写。
LCASE (column_name) 语法相同。
语法: select UCASE(column_name) from table_name;
MID(column_name,start,length) 函数用于从文本字段中提取字符。
column_name : 必须 要提取字符的字段名
start: 必须 规定开始位置
Length: 可选 规定要返回的字符数 省略则是返回剩下剩下所有字符。
语法: select MID(column_name,start,length) from table_name;
LEN(column_name) 返回字段中文本的长度(多少个字符)
语法: sql server : select LEN(column_name) from table_name;
Mysql数据库 : select LENGTH(column_name) from table_name;
ROUND(column_name, decimals 用于把数值字段舍入为指定的小数位数。
decimals: 必须 表示 规定要返回的小数位数。
语法: select ROUND(column_name, decimals)from table_name;
NOW() 函数返回当前系统的日期和时间。
语法: select NOW() from table_name;

FORMAT() 函数用于对字段的显示进行格式化。
语法: select FORMAT(column_name,format) from table_name;
示例:select name, url, date_FORMAT(Now(),'%Y-%m-%d') AS date
from Websites;

发布了26 篇原创文章 · 获赞 0 · 访问量 9937

Guess you like

Origin blog.csdn.net/weixin_38246518/article/details/78696230