发布于 2015-07-25 11:37:18 | 427 次阅读 | 评论: 0 | 来源: 网络整理

SQLite有许多内置函数,字符串或数字数据进行处理。以下是一些有用的SQLite内置函数和所有的大小不敏感,这意味着可以使用这些函数无论是小写形式或大写或混合形式。欲了解更多详情,您可以检查SQLite的官方文档:

S.N. 函数及说明
1 SQLite COUNT Function
The SQLite COUNT aggregate function is used to count the number of rows in a database table.
2 SQLite MAX Function 
The SQLite MAX aggregate function allows us to select the highest (maximum) value for a certain column.
3 SQLite MIN Function
The SQLite MIN aggregate function allows us to select the lowest (minimum) value for a certain column.
4 SQLite AVG Function 
The SQLite AVG aggregate function selects the average value for certain table column.
5 SQLite SUM Function
The SQLite SUM aggregate function allows selecting the total for a numeric column.
6 SQLite RANDOM Function
The SQLite RANDOM function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807.
7 SQLite ABS Function
The SQLite ABS function returns the absolute value of the numeric argument.
8 SQLite UPPER Function
The SQLite UPPER function converts a string into upper-case letters.
9 SQLite LOWER Function
The SQLite LOWER function converts a string into lower-case letters.
10 SQLite LENGTH Function
The SQLite LENGTH function returns the length of a string.
11 SQLite sqlite_version Function
The SQLite sqlite_version function returns the version of the SQLite library.

在我们开始之前提供上述函数的例子,考虑COMPANY表有以下记录:


ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

SQLite COUNT 函数

SQLite的COUNT聚集函数是用来计算一个数据库表中的行数。下面的例子:


sqlite> SELECT count(*) FROM COMPANY;

以上的SQLite SQL语句将产生以下结果:


count(*)
----------
7

SQLite MAX 函数

SQLite的MAX聚合函数允许我们选择某列的最高值(最大)。下面的例子:


sqlite> SELECT max(salary) FROM COMPANY;

以上的SQLite SQL语句将产生以下结果:


max(salary)
-----------
85000.0

SQLite MIN 函数

SQLite的MIN聚合函数允许我们选择某列最低值(最小)。下面的例子:


sqlite> SELECT min(salary) FROM COMPANY;

以上的SQLite SQL语句将产生以下结果:


min(salary)
-----------
10000.0

SQLite AVG 函数

SQLite的AVG聚合函数选择若干表列的平均值。下面的例子:


sqlite> SELECT avg(salary) FROM COMPANY;

以上的SQLite SQL语句将产生以下结果:


avg(salary)
----------------
37142.8571428572

SQLite SUM 函数

SQLite的SUM聚合函数允许为一个数值列共选择。下面的例子:


sqlite> SELECT sum(salary) FROM COMPANY;

以上的SQLite SQL语句将产生以下结果:


sum(salary)
-----------
260000.0

SQLite RANDOM 函数

SQLite 随机函数返回一个伪随机整数介于-9223372036854775808和9223372036854775807之间。下面的例子:


sqlite> SELECT random() AS Random;

以上的SQLite SQL语句将产生以下结果:


Random
-------------------
5876796417670984050

SQLite ABS 函数

SQLite 的ABS函数返回数值参数的绝对值。下面的例子:


sqlite> SELECT abs(5), abs(-15), abs(NULL), abs(0), abs("ABC");

以上的SQLite SQL语句将产生以下结果:


abs(5)      abs(-15)    abs(NULL)   abs(0)      abs("ABC")
----------  ----------  ----------  ----------  ----------
5           15                      0           0.0

SQLite UPPER Function

The SQLite UPPER function converts a string into upper-case letters. Following is the example:


sqlite> SELECT upper(name) FROM COMPANY;

以上的SQLite SQL语句将产生以下结果:


upper(name)
-----------
PAUL
ALLEN
TEDDY
MARK
DAVID
KIM
JAMES

SQLite LOWER 函数

SQLite 的LOWER函数将字符串转换成小写字母。下面的例子:


sqlite> SELECT lower(name) FROM COMPANY;

以上的SQLite SQL语句将产生以下结果:


lower(name)
-----------
paul
allen
teddy
mark
david
kim
james

SQLite LENGTH 函数

SQLite的长度函数返回字符串的长度。下面的例子:


sqlite> SELECT name, length(name) FROM COMPANY;

以上的SQLite SQL语句将产生以下结果:


NAME        length(name)
----------  ------------
Paul        4
Allen       5
Teddy       5
Mark        4
David       5
Kim         3
James       5

SQLite sqlite_version 函数

SQLite sqlite_version的函数返回SQLite库的版本。下面的例子:


sqlite> SELECT sqlite_version() AS 'SQLite Version';

以上的SQLite SQL语句将产生以下结果:


SQLite Version
--------------
3.6.20
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务