数据库中的函数研究 1)SQL 拥有很多可用于计数和计算的内建函数 2)常用函数: 3)avg() 返回数值列的平均值 语法:select avg(列名)from table_name 示例: select avg(price) as priceAverage from table_name select ProductName, Price from table_name where Price > (select AVG(Price) from Products) #选择价格高于平均价格的 "ProductName" 和 "Price" 记录 4)count() 返回匹配指定条件的行数 语法: select COUNT(column_name) FROM table_name select COUNT(*) FROM table_name select COUNT(distinct column_name) FROM table_name #返回指定列的不同值的数量 示例: select COUNT(CustomerID) AS OrdersFromCustomerID7 from Orders WHERE CustomerID=7 #计算 "Orders" 表中 "CustomerID"=7 的订单数 SELECT COUNT(*) AS NumberOfOrders FROM Orders #计算 "Orders" 表中的订单总数 SELECT COUNT(DISTINCT CustomerID) AS NumberOfCustomers FROM Orders #计算 "Orders" 表中不同客户的数目 5)first() 返回指定的列中第一个记录的值 语法: SELECT FIRST(column_name) FROM table_name SELECT TOP 1 column_name FROM table_name ORDER BY column_name ASC SELECT column_name FROM table_name ORDER BY column_name ASC limit 1 示例: 6)last() 返回指定的列中最后一个记录的值 语法: SELECT LAST(column_name) FROM table_name SELECT LAST(CustomerName) AS LastCustomer FROM Customers SELECT TOP 1 column_name FROM table_name ORDER BY column_name DESC #加入desc表示取最后一个数据 SELECT column_name FROM table_name ORDER BY column_name DESC LIMIT 1 示例: 7)MAX() 返回指定列的最大值 语法:SELECT MAX(column_name) FROM table_name 示例:SELECT MAX(Price) AS HighestPrice FROM Products 8)MIN() 返回指定列的最小值 语法:SELECT MIN(column_name) FROM table_name 示例:SELECT MIN(Price) AS SmallestOrderPrice FROM Products#从 "Products" 表的 "Price" 列获取最小值 9)SUM() 返回数值列的总数 语法:SELECT SUM(column_name) FROM table_name 示例:SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails#查找 "OrderDetails" 表的 "Quantity" 字段的总数 10)GROUP BY() GROUP BY 语句用于结合 Aggregate 函数,根据一个或多个列对结果集进行分组 aggregate_function:函数 column_name operator value:表达式 语法:select column_name, aggregate_function(column_name) FROM 表名 WHERE column_name operator value GROUP BY column_name 示例: 特殊说明:该语句需要单独研究 11)having() 特殊说明:该语句需要单独研究 12)ucase() ucase() 函数把字段的值转换为大写 语法:SELECT UCASE(column_name) FROM table_name SELECT UPPER(column_name) FROM table_name 示例:SELECT UCASE(CustomerName) AS Customer, City FROM Customers #从 "Customers" 表中选取 "CustomerName" 和 "City" 列,并把 "CustomerName" 列的值转换为大写 #从Customers表中取2个数据列出来,取city和CustomerName这2个列的数据,并将CustomerName更名为Customer名 13)lcase() LCASE() 函数把字段的值转换为小写。 语法:SELECT LCASE(column_name) FROM table_name SELECT LOWER(column_name) FROM table_name 示例:SELECT LCASE(CustomerName) AS Customer, City FROM Customers #从 "Customers" 表中选取 "CustomerName" 和 "City" 列,并把 "CustomerName" 列的值转换为小写 14)MID() MID() 函数用于从文本字段中提取字符。 语法: SELECT MID(column_name,start[,length]) FROM table_name 参数 描述 column_name 必需。要提取字符的字段。 start 必需。规定开始位置(起始值是 1)。 length 可选。要返回的字符数。如果省略,则 MID() 函数返回剩余文本。 示例:SELECT MID(City,1,4) AS ShortCity FROM Customers #从 "Customers" 表的 "City" 列中提取前 4 个字符 15)LEN() LEN() 函数返回文本字段中值的长度 语法:SELECT LEN(column_name) FROM table_name 示例:SELECT CustomerName,LEN(Address) as LengthOfAddress FROM Customers #从 "Customers" 表中选取 "CustomerName" 和 "Address" 列中值的长度 16)ROUND() round() 函数用于把数值字段舍入为指定的小数位数 语法: select ROUND(column_name,decimals) from table_name 参数 描述 column_name 必需。要舍入的字段。 decimals 必需。规定要返回的小数位数。 示例:SELECT ProductName, ROUND(Price,0) AS RoundedPrice FROM Products #从 "Products" 表中选取产品名称和价格舍入为最接近的整数。 17)NOW() NOW() 函数返回当前系统的日期和时间 语法:SELECT NOW() FROM table_name 示例:SELECT ProductName, Price, Now() AS PerDate FROM Products #从 "Products" 表中选取产品名称以及当天的价格 18)FORMAT() FORMAT() 函数用于对字段的显示进行格式化 语法:SELECT FORMAT(column_name,format) FROM table_name 参数 描述 column_name 必需。要格式化的字段 format 必需。规定格式 示例:SELECT ProductName, Price, FORMAT(Now(),‘YYYY-MM-DD‘) AS PerDate FROM Products #从 "Products" 表中选取产品名称以及当天(格式化为 YYYY-MM-DD)的价格
时间: 2024-10-25 12:02:14