Sqlite 简明教程

SQLite - Useful Functions

SQLite 拥有许多内置函数来对字符串或数字数据执行处理。以下是几个有用的 SQLite 内置函数的列表,它们都区分大小写,这意味着你可以用小写形式、大写形式或混合形式使用这些函数。有关更多详细信息,你可以查看 SQLite 的官方文档。

SQLite has many built-in functions to perform processing on string or numeric data. Following is the list of few useful SQLite built-in functions and all are case in-sensitive which means you can use these functions either in lower-case form or in upper-case or in mixed form. For more details, you can check official documentation for SQLite.

Sr.No.

Function & Description

1

SQLite COUNT Function SQLite COUNT aggregate function is used to count the number of rows in a database table.

2

*SQLite MAX Function * SQLite MAX aggregate function allows us to select the highest (maximum) value for a certain column.

3

SQLite MIN Function SQLite MIN aggregate function allows us to select the lowest (minimum) value for a certain column.

4

*SQLite AVG Function * SQLite AVG aggregate function selects the average value for certain table column.

5

SQLite SUM Function SQLite SUM aggregate function allows selecting the total for a numeric column.

6

SQLite RANDOM Function SQLite RANDOM function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807.

7

SQLite ABS Function SQLite ABS function returns the absolute value of the numeric argument.

8

SQLite UPPER Function SQLite UPPER function converts a string into upper-case letters.

9

SQLite LOWER Function SQLite LOWER function converts a string into lower-case letters.

10

SQLite LENGTH Function SQLite LENGTH function returns the length of a string.

11

SQLite sqlite_version Function SQLite sqlite_version function returns the version of the SQLite library.

在我们开始提供上述函数示例之前,请考虑包含以下记录的 COMPANY 表。

Before we start giving examples on the above-mentioned functions, consider COMPANY table with the following records.

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 Function

SQLite COUNT 聚集函数用于统计数据库表中的行数。以下是一个示例 −

SQLite COUNT aggregate function is used to count the number of rows in a database table. Following is an example −

sqlite> SELECT count(*) FROM COMPANY;

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite MAX Function

SQLite MAX 聚集函数允许我们选择某个列的最高(最大)值。以下是一个示例 −

SQLite MAX aggregate function allows us to select the highest (maximum) value for a certain column. Following is an example −

sqlite> SELECT max(salary) FROM COMPANY;

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite MIN Function

SQLite MIN 聚集函数允许我们选择某个列的最低(最小)值。以下是一个示例 −

SQLite MIN aggregate function allows us to select the lowest (minimum) value for a certain column. Following is an example −

sqlite> SELECT min(salary) FROM COMPANY;

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite AVG Function

SQLite AVG 聚集函数选择某个表列的平均值。以下是一个示例 −

SQLite AVG aggregate function selects the average value for a certain table column. Following is an the example −

sqlite> SELECT avg(salary) FROM COMPANY;

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite SUM Function

SQLite SUM 聚合函数允许选择数字列的总和。以下是一个示例:

SQLite SUM aggregate function allows selecting the total for a numeric column. Following is an example −

sqlite> SELECT sum(salary) FROM COMPANY;

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite RANDOM Function

SQLite RANDOM 函数返回 -9223372036854775808 和 +9223372036854775807 之间的伪随机整数。以下是一个示例:

SQLite RANDOM function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807. Following is an example −

sqlite> SELECT random() AS Random;

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite ABS Function

SQLite ABS 函数返回数字参数的绝对值。以下是一个示例:

SQLite ABS function returns the absolute value of the numeric argument. Following is an example −

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

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite UPPER Function

SQLite UPPER 函数将字符串转换为大写字母。以下是一个示例:

SQLite UPPER function converts a string into upper-case letters. Following is an example −

sqlite> SELECT upper(name) FROM COMPANY;

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite LOWER Function

SQLite LOWER 函数将字符串转换为小写字母。以下是一个示例:

SQLite LOWER function converts a string into lower-case letters. Following is an example −

sqlite> SELECT lower(name) FROM COMPANY;

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite LENGTH Function

SQLite LENGTH 函数返回字符串的长度。以下是一个示例:

SQLite LENGTH function returns the length of a string. Following is an example −

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

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

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

SQLite sqlite_version Function

SQLite sqlite_version 函数返回 SQLite 库的版本。以下是一个示例:

SQLite sqlite_version function returns the version of the SQLite library. Following is an example −

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

上述 SQLite SQL 语句将产生以下结果。

The above SQLite SQL statement will produce the following.

SQLite Version
--------------
3.6.20