Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1446768
  • 博文数量: 239
  • 博客积分: 5909
  • 博客等级: 大校
  • 技术积分: 2715
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-24 20:19
文章分类

全部博文(239)

文章存档

2014年(4)

2013年(22)

2012年(140)

2011年(14)

2010年(59)

我的朋友

分类: 数据库开发技术

2010-08-19 12:55:07

sql函数的语法:select function(列) from 表
分为aggregate函数(针对多值操作)和scalar函数(针对单值操作)。
如果select语句中有select语句嵌套,则必须使用group by

avg():

SELECT AVG(column_name) FROM table_name


count():

SELECT COUNT(column_name) FROM table_name  --or (distinct column_name)


first() or last():

SELECT FIRST(column_name) FROM table_name
SELECT LAST(column_name) FROM table_name


max() or min() or sum():

SELECT MAX(column_name) FROM table_name
SELECT MIN(column_name) FROM table_name
SELECT SUM(column_name) FROM table_name


group by:

SELECT column_name, aggregate_function(column_name) FROM table_name
WHERE column_name operator value GROUP BY column_name --按某个列名进行操作


having:

SELECT column_name, aggregate_function(column_name) FROM table_name
WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value

for example
SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Bush' OR Customer='Adams' GROUP BY Customer HAVING SUM(OrderPrice)>1500


ucase() or lcase():

SELECT UCASE(column_name) FROM table_name
SELECT LCASE(column_name) FROM table_name


mid() or len():

SELECT MID(column_name,start[,length]) FROM table_name --mid:提取字符

 SELECT LEN(column_name) FROM table_name --len:返回文本长度

round():

SELECT ROUND(column,decimals) FROM table_name --:decimals:小数位,四舍五入
SELECT ProductName, ROUND(UnitPrice,0) as UnitPrice FROM Products


now():

SELECT NOW() FROM table_name  --显示当前时间


format():

SELECT FORMAT(column_name,format) FROM table_name

SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate
FROM Products


阅读(916) | 评论(0) | 转发(0) |
0

上一篇:学习sql(提高篇)

下一篇:XML与HTML的比较

给主人留下些什么吧!~~