标签:
avg()
select avg(orderPrice) As OrderAverage from orders
select customer from orders where orderPrice>(select avg(OrderPrice) from Orders)
count()
select count(column_name) from table_name
select count(distinct column_name) from table_name
select count(customer) as customerNilsen from orders where customer=‘Carter‘
select count(*) as numberofOrders from orders
select count(distinct customer) as numberofcustomers from orders
first()
first() 函数返回指定的字段中第一个记录的值。
select first(OrderPrice) As FirstOrderPrice from Orders
last()
LAST() 函数返回指定的字段中最后一个记录的值。
select last(OrderPrice) As LastOrderPrice from orders
max()
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
min()
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
sum()
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
group by
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer
group by 一个以上的列
select Customer,OrderDate,SUM(OrderPrice) FROM Orders
group byCustomer,OrderDate
having子句
在 SQL 中增加 having 子句原因是,where 关键字无法与合计函数一起使用
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000
SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer=‘Bush‘ OR Customer=‘Adams‘ GROUP BY Customer HAVING SUM(OrderPrice)>1500
ucase()函数
ucase 函数把字段的值转换为大写。
SELECT UCASE(LastName) as LastName,FirstName FROM Persons
lcase() 函数
lcase() 函数把字段的值转换为小写。
SELECT LCASE(LastName) as LastName,FirstName FROM Persons
Mid() 函数
Mid 函数用于从文本字段中提取字符。
SELECT MID(City,1,3) as SmallCity FROM Persons
round() 函数
Round 函数用于把数值字段舍入为指定的小数位数。
SELECT ProductName, ROUND(UnitPrice,0) as UnitPrice FROM Products
Now() 函数
Now 函数返回当前的日期和时间。
SELECT ProductName, UnitPrice, Now() as PerDate FROM Products
format() 函数
format 函数用于对字段的显示进行格式化。
SELECT ProductName, UnitPrice, FORMAT(Now(),‘YYYY-MM-DD‘) as PerDate FROM Products
标签:
原文地址:http://my.oschina.net/u/2356966/blog/489346