SQLite 有许多内置函数用于处理字符串或数字数据。下面列出了一些有用的 SQLite 内置函数,且所有函数都是大小写不敏感,这意味着您可以使用这些函数的小写形式或大写形式或混合形式。欲了解更多详情,请查看 SQLite 的官方文档: 序号 函数 & 描述 1 SQLite COUNT 函数 SQLite COUNT 聚集函数是用来计算一个数据库表中的行数。 2 SQLite MAX 函数 SQLite MAX 聚合函数允许我们选择某列的最大值。 3 SQLite MIN 函数 SQLite MIN 聚合函数允许我们选择某列的最小值。 4 SQLite AVG 函数 SQLite AVG 聚合函数计算某列的平均值。 5 SQLite SUM 函数 SQLite SUM 聚合函数允许为一个数值列计算总和。 6 SQLite RANDOM 函数 SQLite RANDOM 函数返回一个介于 -9223372036854775808 和 +9223372036854775807 之间的伪随机整数。 7 SQLite ABS 函数 SQLite ABS 函数返回数值参数的绝对值。 8 SQLite UPPER 函数 SQLite UPPER 函数把字符串转换为大写字母。 9 SQLite LOWER 函数 SQLite LOWER 函数把字符串转换为小写字母。 10 SQLite LENGTH 函数 SQLite LENGTH 函数返回字符串的长度。 11 SQLite sqlite_version 函数 […]
View Details如果您的站点允许用户通过网页输入,并将输入内容插入到 SQLite 数据库中,这个时候您就面临着一个被称为 SQL 注入的安全问题。本章节将向您讲解如何防止这种情况的发生,确保脚本和 SQLite 语句的安全。 注入通常在请求用户输入时发生,比如需要用户输入姓名,但用户却输入了一个 SQLite 语句,而这语句就会在不知不觉中在数据库上运行。 永远不要相信用户提供的数据,所以只处理通过验证的数据,这项规则是通过模式匹配来完成的。在下面的实例中,用户名 username 被限制为字母数字字符或者下划线,长度必须在 8 到 20 个字符之间 – 请根据需要修改这些规则。
1 2 3 4 5 6 |
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">preg_match</span><span class="pun">(</span><span class="str">"/^\w{8,20}$/"</span><span class="pun">,</span><span class="pln"> $_GET</span><span class="pun">[</span><span class="str">'username'</span><span class="pun">],</span><span class="pln"> $matches</span><span class="pun">)){</span><span class="pln"> $db </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">SQLiteDatabase</span><span class="pun">(</span><span class="str">'filename'</span><span class="pun">);</span><span class="pln"> $result </span><span class="pun">=</span> <span class="lit">@$db</span><span class="pun">-></span><span class="pln">query</span><span class="pun">(</span><span class="str">"SELECT * FROM users WHERE username=$matches[0]"</span><span class="pun">);</span> <span class="pun">}</span><span class="kwd">else</span><span class="pun">{</span><span class="pln"> echo </span><span class="str">"username not accepted"</span><span class="pun">;</span> <span class="pun">}</span> |
为了演示这个问题,假设考虑此摘录:To demonstrate the problem, consider this excerpt:
1 2 |
<span class="pln">$name </span><span class="pun">=</span> <span class="str">"Qadir'; DELETE FROM users;"</span><span class="pun">;</span> <span class="lit">@$db</span><span class="pun">-></span><span class="pln">query</span><span class="pun">(</span><span class="str">"SELECT * FROM users WHERE username='{$name}'"</span><span class="pun">);</span> |
函数调用是为了从用户表中检索 name 列与用户指定的名称相匹配的记录。正常情况下,$name 只包含字母数字字符或者空格,比如字符串 ilia。但在这里,向 $name 追加了一个全新的查询,这个对数据库的调用将会造成灾难性的问题:注入的 DELETE 查询会删除 users 的所有记录。 虽然已经存在有不允许查询堆叠或在单个函数调用中执行多个查询的数据库接口,如果尝试堆叠查询,则会调用失败,但 SQLite 和 PostgreSQL 里仍进行堆叠查询,即执行在一个字符串中提供的所有查询,这会导致严重的安全问题。 防止 SQL 注入 在脚本语言中,比如 PERL 和 PHP,您可以巧妙地处理所有的转义字符。编程语言 PHP 提供了字符串函数 sqlite_escape_string() 来转义对于 SQLite 来说比较特殊的输入字符。
1 2 3 4 5 |
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">get_magic_quotes_gpc</span><span class="pun">())</span> <span class="pun">{</span><span class="pln"> $name </span><span class="pun">=</span><span class="pln"> sqlite_escape_string</span><span class="pun">(</span><span class="pln">$name</span><span class="pun">);</span> <span class="pun">}</span><span class="pln"> $result </span><span class="pun">=</span> <span class="lit">@$db</span><span class="pun">-></span><span class="pln">query</span><span class="pun">(</span><span class="str">"SELECT * FROM users WHERE username='{$name}'"</span><span class="pun">);</span> |
虽然编码使得插入数据变得安全,但是它会呈现简单的文本比较,在查询中,对于包含二进制数据的列,LIKE 子句是不可用的。 请注意,addslashes() 不应该被用在 SQLite 查询中引用字符串,它会在检索数据时导致奇怪的结果。 from:http://www.runoob.com/sqlite/sqlite-injection.html
View DetailsSQLite 的 AUTOINCREMENT 是一个关键字,用于表中的字段值自动递增。我们可以在创建表时在特定的列名称上使用 AUTOINCREMENT 关键字实现该字段值的自动增加。 关键字 AUTOINCREMENT 只能用于整型(INTEGER)字段。 语法 AUTOINCREMENT 关键字的基本用法如下:
1 2 3 4 5 6 7 |
<span class="pln">CREATE TABLE table_name</span><span class="pun">(</span><span class="pln"> column1 INTEGER AUTOINCREMENT</span><span class="pun">,</span><span class="pln"> column2 datatype</span><span class="pun">,</span><span class="pln"> column3 datatype</span><span class="pun">,</span> <span class="pun">.....</span><span class="pln"> columnN datatype</span><span class="pun">,</span> <span class="pun">);</span> |
实例 假设要创建的 COMPANY 表如下所示:
1 2 3 4 5 6 7 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> CREATE TABLE COMPANY</span><span class="pun">(</span><span class="pln"> ID INTEGER PRIMARY KEY AUTOINCREMENT</span><span class="pun">,</span><span class="pln"> NAME TEXT NOT NULL</span><span class="pun">,</span><span class="pln"> AGE INT NOT NULL</span><span class="pun">,</span><span class="pln"> ADDRESS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln"> SALARY REAL </span><span class="pun">);</span> |
现在,向 COMPANY 表插入以下记录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="pln">INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span> <span class="str">'Paul'</span><span class="pun">,</span> <span class="lit">32</span><span class="pun">,</span> <span class="str">'California'</span><span class="pun">,</span> <span class="lit">20000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="str">'Allen'</span><span class="pun">,</span> <span class="lit">25</span><span class="pun">,</span> <span class="str">'Texas'</span><span class="pun">,</span> <span class="lit">15000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="str">'Teddy'</span><span class="pun">,</span> <span class="lit">23</span><span class="pun">,</span> <span class="str">'Norway'</span><span class="pun">,</span> <span class="lit">20000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span> <span class="str">'Mark'</span><span class="pun">,</span> <span class="lit">25</span><span class="pun">,</span> <span class="str">'Rich-Mond '</span><span class="pun">,</span> <span class="lit">65000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span> <span class="str">'David'</span><span class="pun">,</span> <span class="lit">27</span><span class="pun">,</span> <span class="str">'Texas'</span><span class="pun">,</span> <span class="lit">85000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span> <span class="str">'Kim'</span><span class="pun">,</span> <span class="lit">22</span><span class="pun">,</span> <span class="str">'South-Hall'</span><span class="pun">,</span> <span class="lit">45000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY </span><span class="pun">(</span><span class="pln">NAME</span><span class="pun">,</span><span class="pln">AGE</span><span class="pun">,</span><span class="pln">ADDRESS</span><span class="pun">,</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span> <span class="str">'James'</span><span class="pun">,</span> <span class="lit">24</span><span class="pun">,</span> <span class="str">'Houston'</span><span class="pun">,</span> <span class="lit">10000.00</span> <span class="pun">);</span> |
这将向 COMPANY 表插入 7 个元组,此时 COMPANY 表的记录如下:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
from:http://www.runoob.com/sqlite/sqlite-autoincrement.html
View Details事务(Transaction)是一个对数据库执行工作单元。事务(Transaction)是以逻辑顺序完成的工作单位或序列,可以是由用户手动操作完成,也可以是由某种数据库程序自动完成。 事务(Transaction)是指一个或多个更改数据库的扩展。例如,如果您正在创建一个记录或者更新一个记录或者从表中删除一个记录,那么您正在该表上执行事务。重要的是要控制事务以确保数据的完整性和处理数据库错误。 实际上,您可以把许多的 SQLite 查询联合成一组,把所有这些放在一起作为事务的一部分进行执行。 事务的属性 事务(Transaction)具有以下四个标准属性,通常根据首字母缩写为 ACID: 原子性(Atomicity):确保工作单位内的所有操作都成功完成,否则,事务会在出现故障时终止,之前的操作也会回滚到以前的状态。 一致性(Consistency):确保数据库在成功提交的事务上正确地改变状态。 隔离性(Isolation):使事务操作相互独立和透明。 持久性(Durability):确保已提交事务的结果或效果在系统发生故障的情况下仍然存在。 事务控制 使用下面的命令来控制事务: BEGIN TRANSACTION:开始事务处理。 COMMIT:保存更改,或者可以使用 END TRANSACTION 命令。 ROLLBACK:回滚所做的更改。 事务控制命令只与 DML 命令 INSERT、UPDATE 和 DELETE 一起使用。他们不能在创建表或删除表时使用,因为这些操作在数据库中是自动提交的。 BEGIN TRANSACTION 命令 事务(Transaction)可以使用 BEGIN TRANSACTION 命令或简单的 BEGIN 命令来启动。此类事务通常会持续执行下去,直到遇到下一个 COMMIT 或 ROLLBACK 命令。不过在数据库关闭或发生错误时,事务处理也会回滚。以下是启动一个事务的简单语法:
1 2 3 4 5 |
<span class="kwd">BEGIN</span><span class="pun">;</span> <span class="kwd">or</span> <span class="kwd">BEGIN</span><span class="pln"> TRANSACTION</span><span class="pun">;</span> |
COMMIT 命令 COMMIT 命令是用于把事务调用的更改保存到数据库中的事务命令。 COMMIT 命令把自上次 COMMIT 或 ROLLBACK 命令以来的所有事务保存到数据库。 COMMIT 命令的语法如下:
1 2 3 4 5 |
<span class="pln">COMMIT</span><span class="pun">;</span> <span class="kwd">or</span> <span class="kwd">END</span><span class="pln"> TRANSACTION</span><span class="pun">;</span> |
ROLLBACK 命令 ROLLBACK 命令是用于撤消尚未保存到数据库的事务的事务命令。 ROLLBACK 命令只能用于撤销自上次发出 COMMIT 或 ROLLBACK 命令以来的事务。 ROLLBACK 命令的语法如下:
1 |
<span class="pln">ROLLBACK</span><span class="pun">;</span> |
实例 假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
现在,让我们开始一个事务,并从表中删除 age = 25 的记录,最后,我们使用 ROLLBACK 命令撤消所有的更改。
1 2 3 |
<span class="pln">sqlite</span><span class="pun">></span> <span class="kwd">BEGIN</span><span class="pun">;</span><span class="pln"> sqlite</span><span class="pun">></span><span class="pln"> DELETE FROM COMPANY WHERE AGE </span><span class="pun">=</span> <span class="lit">25</span><span class="pun">;</span><span class="pln"> sqlite</span><span class="pun">></span><span class="pln"> ROLLBACK</span><span class="pun">;</span> |
检查 COMPANY 表,仍然有以下记录:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
现在,让我们开始另一个事务,从表中删除 age = […]
View Details索引(Index)是一种特殊的查找表,数据库搜索引擎用来加快数据检索。简单地说,索引是一个指向表中数据的指针。一个数据库中的索引与一本书后边的索引是非常相似的。 例如,如果您想在一本讨论某个话题的书中引用所有页面,您首先需要指向索引,索引按字母顺序列出了所有主题,然后指向一个或多个特定的页码。 索引有助于加快 SELECT 查询和 WHERE 子句,但它会减慢使用 UPDATE 和 INSERT 语句时的数据输入。索引可以创建或删除,但不会影响数据。 使用 CREATE INDEX 语句创建索引,它允许命名索引,指定表及要索引的一列或多列,并指示索引是升序排列还是降序排列。 索引也可以是唯一的,与 UNIQUE 约束类似,在列上或列组合上防止重复条目。 CREATE INDEX 命令 CREATE INDEX 的基本语法如下:
1 |
<span class="pln">CREATE INDEX index_name ON table_name</span><span class="pun">;</span> |
单列索引 单列索引是一个只基于表的一个列上创建的索引。基本语法如下:
1 2 |
<span class="pln">CREATE INDEX index_name ON table_name </span><span class="pun">(</span><span class="pln">column_name</span><span class="pun">);</span> |
唯一索引 使用唯一索引不仅是为了性能,同时也为了数据的完整性。唯一索引不允许任何重复的值插入到表中。基本语法如下:
1 2 |
<span class="pln">CREATE UNIQUE INDEX index_name on table_name </span><span class="pun">(</span><span class="pln">column_name</span><span class="pun">);</span> |
组合索引 组合索引是基于一个表的两个或多个列上创建的索引。基本语法如下:
1 2 |
<span class="pln">CREATE INDEX index_name on table_name </span><span class="pun">(</span><span class="pln">column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">);</span> |
是否要创建一个单列索引还是组合索引,要考虑到您在作为查询过滤条件的 WHERE 子句中使用非常频繁的列。 如果值使用到一个列,则选择使用单列索引。如果在作为过滤的 WHERE 子句中有两个或多个列经常使用,则选择使用组合索引。 隐式索引 隐式索引是在创建对象时,由数据库服务器自动创建的索引。索引自动创建为主键约束和唯一约束。 实例 下面是一个例子,我们将在 COMPANY 表的 salary 列上创建一个索引:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> CREATE INDEX salary_index ON COMPANY </span><span class="pun">(</span><span class="pln">salary</span><span class="pun">);</span> |
现在,让我们使用 .indices 命令列出 COMPANY 表上所有可用的索引,如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span> <span class="pun">.</span><span class="pln">indices COMPANY</span> |
这将产生如下结果,其中 sqlite_autoindex_COMPANY_1 是创建表时创建的隐式索引。
1 2 |
<span class="pln">salary_index sqlite_autoindex_COMPANY_1</span> |
您可以列出数据库范围的所有索引,如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM sqlite_master WHERE type </span><span class="pun">=</span> <span class="str">'index'</span><span class="pun">;</span> |
DROP INDEX 命令 一个索引可以使用 SQLite 的 DROP 命令删除。当删除索引时应特别注意,因为性能可能会下降或提高。 基本语法如下:
1 |
<span class="pln">DROP INDEX index_name</span><span class="pun">;</span> |
您可以使用下面的语句来删除之前创建的索引:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> DROP INDEX salary_index</span><span class="pun">;</span> |
什么情况下要避免使用索引? 虽然索引的目的在于提高数据库的性能,但这里有几个情况需要避免使用索引。使用索引时,应重新考虑下列准则: 索引不应该使用在较小的表上。 索引不应该使用在有频繁的大批量的更新或插入操作的表上。 索引不应该使用在含有大量的 NULL 值的列上。 索引不应该使用在频繁操作的列上。 from:http://www.runoob.com/sqlite/sqlite-index.html
View DetailsSQLite的 UNION 子句/运算符用于合并两个或多个 SELECT 语句的结果,不返回任何重复的行。 为了使用 UNION,每个 SELECT 被选择的列数必须是相同的,相同数目的列表达式,相同的数据类型,并确保它们有相同的顺序,但它们不必具有相同的长度。 语法 UNION 的基本语法如下:
1 2 3 4 5 6 7 8 9 |
<span class="pln">SELECT column1 </span><span class="pun">[,</span><span class="pln"> column2 </span><span class="pun">]</span><span class="pln"> FROM table1 </span><span class="pun">[,</span><span class="pln"> table2 </span><span class="pun">]</span> <span class="pun">[</span><span class="pln">WHERE condition</span><span class="pun">]</span><span class="pln"> UNION SELECT column1 </span><span class="pun">[,</span><span class="pln"> column2 </span><span class="pun">]</span><span class="pln"> FROM table1 </span><span class="pun">[,</span><span class="pln"> table2 </span><span class="pun">]</span> <span class="pun">[</span><span class="pln">WHERE condition</span><span class="pun">]</span> |
这里给定的条件根据需要可以是任何表达式。 实例 假设有下面两个表,(1)COMPANY 表如下所示:
1 2 3 4 5 6 7 8 9 10 |
<span class="pln">sqlite</span><span class="pun">></span> <span class="kwd">select</span> <span class="pun">*</span> <span class="kwd">from</span><span class="pln"> COMPANY</span><span class="pun">;</span><span class="pln"> ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">--------------------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
(2)另一个表是 DEPARTMENT,如下所示:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID DEPT EMP_ID </span><span class="pun">----------</span> <span class="pun">--------------------</span> <span class="pun">----------</span> <span class="lit">1</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">1</span> <span class="lit">2</span> <span class="typ">Engineering</span> <span class="lit">2</span> <span class="lit">3</span> <span class="typ">Finance</span> <span class="lit">7</span> <span class="lit">4</span> <span class="typ">Engineering</span> <span class="lit">3</span> <span class="lit">5</span> <span class="typ">Finance</span> <span class="lit">4</span> <span class="lit">6</span> <span class="typ">Engineering</span> <span class="lit">5</span> <span class="lit">7</span> <span class="typ">Finance</span> <span class="lit">6</span> |
现在,让我们使用 SELECT 语句及 UNION 子句来连接两个表,如下所示:
1 2 3 4 5 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT EMP_ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> DEPT FROM COMPANY INNER JOIN DEPARTMENT ON COMPANY</span><span class="pun">.</span><span class="pln">ID </span><span class="pun">=</span><span class="pln"> DEPARTMENT</span><span class="pun">.</span><span class="pln">EMP_ID UNION SELECT EMP_ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT ON COMPANY</span><span class="pun">.</span><span class="pln">ID </span><span class="pun">=</span><span class="pln"> DEPARTMENT</span><span class="pun">.</span><span class="pln">EMP_ID</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">EMP_ID NAME DEPT </span><span class="pun">----------</span> <span class="pun">--------------------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="typ">Engineerin</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="typ">Engineerin</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="typ">Finance</span> <span class="lit">5</span> <span class="typ">David</span> <span class="typ">Engineerin</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="typ">Finance</span> <span class="lit">7</span> <span class="typ">James</span> <span class="typ">Finance</span> |
UNION ALL 子句 UNION ALL 运算符用于结合两个 SELECT 语句的结果,包括重复行。 适用于 UNION 的规则同样适用于 UNION ALL 运算符。 语法 UNION ALL 的基本语法如下:
1 2 3 4 5 6 7 8 9 |
<span class="pln">SELECT column1 </span><span class="pun">[,</span><span class="pln"> column2 </span><span class="pun">]</span><span class="pln"> FROM table1 </span><span class="pun">[,</span><span class="pln"> table2 </span><span class="pun">]</span> <span class="pun">[</span><span class="pln">WHERE condition</span><span class="pun">]</span><span class="pln"> UNION ALL SELECT column1 </span><span class="pun">[,</span><span class="pln"> column2 </span><span class="pun">]</span><span class="pln"> FROM table1 </span><span class="pun">[,</span><span class="pln"> table2 </span><span class="pun">]</span> <span class="pun">[</span><span class="pln">WHERE condition</span><span class="pun">]</span> |
这里给定的条件根据需要可以是任何表达式。 实例 现在,让我们使用 SELECT 语句及 UNION ALL 子句来连接两个表,如下所示:
1 2 3 4 5 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT EMP_ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> DEPT FROM COMPANY INNER JOIN DEPARTMENT ON COMPANY</span><span class="pun">.</span><span class="pln">ID </span><span class="pun">=</span><span class="pln"> DEPARTMENT</span><span class="pun">.</span><span class="pln">EMP_ID UNION ALL SELECT EMP_ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> DEPT FROM COMPANY LEFT OUTER JOIN DEPARTMENT ON COMPANY</span><span class="pun">.</span><span class="pln">ID </span><span class="pun">=</span><span class="pln"> DEPARTMENT</span><span class="pun">.</span><span class="pln">EMP_ID</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span class="pln">EMP_ID NAME DEPT </span><span class="pun">----------</span> <span class="pun">--------------------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="typ">Engineerin</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="typ">Engineerin</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="typ">Finance</span> <span class="lit">5</span> <span class="typ">David</span> <span class="typ">Engineerin</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="typ">Finance</span> <span class="lit">7</span> <span class="typ">James</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Paul</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="typ">Engineerin</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="typ">Engineerin</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="typ">Finance</span> <span class="lit">5</span> <span class="typ">David</span> <span class="typ">Engineerin</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="typ">Finance</span> <span class="lit">7</span> <span class="typ">James</span> <span class="typ">Finance from:<a href="http://www.runoob.com/sqlite/sqlite-unions-clause.html">http://www.runoob.com/sqlite/sqlite-unions-clause.html</a></span> |
SQLite 的 Joins 子句用于结合两个或多个数据库中表的记录。JOIN 是一种通过共同值来结合两个表中字段的手段。 SQL 定义了三种主要类型的连接: 交叉连接 – CROSS JOIN 内连接 – INNER JOIN 外连接 – OUTER JOIN 在我们继续之前,让我们假设有两个表 COMPANY 和 DEPARTMENT。我们已经看到了用来填充 COMPANY 表的 INSERT 语句。现在让我们假设 COMPANY 表的记录列表如下:
1 2 3 4 5 6 7 8 9 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
另一个表是 DEPARTMENT,定义如下:
1 2 3 4 5 |
<span class="pln">CREATE TABLE DEPARTMENT</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> DEPT CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">)</span><span class="pln"> NOT NULL</span><span class="pun">,</span><span class="pln"> EMP_ID INT NOT NULL </span><span class="pun">);</span> |
下面是填充 DEPARTMENT 表的 INSERT 语句:
1 2 3 4 5 6 7 8 |
<span class="pln">INSERT INTO DEPARTMENT </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln"> DEPT</span><span class="pun">,</span><span class="pln"> EMP_ID</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span> <span class="str">'IT Billing'</span><span class="pun">,</span> <span class="lit">1</span> <span class="pun">);</span><span class="pln"> INSERT INTO DEPARTMENT </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln"> DEPT</span><span class="pun">,</span><span class="pln"> EMP_ID</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">2</span><span class="pun">,</span> <span class="str">'Engineering'</span><span class="pun">,</span> <span class="lit">2</span> <span class="pun">);</span><span class="pln"> INSERT INTO DEPARTMENT </span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="pln"> DEPT</span><span class="pun">,</span><span class="pln"> EMP_ID</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span> <span class="str">'Finance'</span><span class="pun">,</span> <span class="lit">7</span> <span class="pun">);</span> |
最后,我们在 DEPARTMENT 表中有下列的记录列表:
1 2 3 4 5 |
<span class="pln">ID DEPT EMP_ID </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">1</span> <span class="lit">2</span> <span class="typ">Engineerin</span> <span class="lit">2</span> <span class="lit">3</span> <span class="typ">Finance</span> <span class="lit">7</span> |
交叉连接 – CROSS JOIN 交叉连接(CROSS JOIN)把第一个表的每一行与第二个表的每一行进行匹配。如果两个输入表分别有 x 和 y 列,则结果表有 x+y 列。由于交叉连接(CROSS JOIN)有可能产生非常大的表,使用时必须谨慎,只在适当的时候使用它们。 下面是交叉连接(CROSS JOIN)的语法:
1 |
<span class="pln">SELECT </span><span class="pun">...</span><span class="pln"> FROM table1 CROSS JOIN table2 </span><span class="pun">...</span> |
基于上面的表,我们可以写一个交叉连接(CROSS JOIN),如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT EMP_ID</span><span class="pun">,</span><span class="pln"> NAME</span><span class="pun">,</span><span class="pln"> DEPT FROM COMPANY CROSS JOIN DEPARTMENT</span><span class="pun">;</span> |
上面的查询会产生以下结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span class="pln">EMP_ID NAME DEPT </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Paul</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Paul</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Allen</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Allen</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Teddy</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Teddy</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Teddy</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Mark</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Mark</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Mark</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">David</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">David</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">David</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">Kim</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">Kim</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">Kim</span> <span class="typ">Finance</span> <span class="lit">1</span> <span class="typ">James</span><span class="pln"> IT </span><span class="typ">Billing</span> <span class="lit">2</span> <span class="typ">James</span> <span class="typ">Engineerin</span> <span class="lit">7</span> <span class="typ">James</span> <span class="typ">Finance</span> |
内连接 – INNER JOIN 内连接(INNER JOIN)根据连接谓词结合两个表(table1 和 table2)的列值来创建一个新的结果表。查询会把 table1 中的每一行与 table2 中的每一行进行比较,找到所有满足连接谓词的行的匹配对。当满足连接谓词时,A 和 B 行的每个匹配对的列值会合并成一个结果行。 内连接(INNER JOIN)是最常见的连接类型,是默认的连接类型。INNER 关键字是可选的。 下面是内连接(INNER JOIN)的语法:
1 |
<span class="pln">SELECT </span><span class="pun">...</span><span class="pln"> FROM table1 </span><span class="pun">[</span><span class="pln">INNER</span><span class="pun">]</span><span class="pln"> JOIN table2 ON conditional_expression </span><span class="pun">...</span> |
为了避免冗余,并保持较短的措辞,可以使用 USING 表达式声明内连接(INNER […]
View Details约束是在表的数据列上强制执行的规则。这些是用来限制可以插入到表中的数据类型。这确保了数据库中数据的准确性和可靠性。 约束可以是列级或表级。列级约束仅适用于列,表级约束被应用到整个表。 以下是在 SQLite 中常用的约束。 NOT NULL 约束:确保某列不能有 NULL 值。 DEFAULT 约束:当某列没有指定值时,为该列提供默认值。 UNIQUE 约束:确保某列中的所有值是不同的。 PRIMARY Key 约束:唯一标识数据库表中的各行/记录。 CHECK 约束:CHECK 约束确保某列中的所有值满足一定条件。 NOT NULL 约束 默认情况下,列可以保存 NULL 值。如果您不想某列有 NULL 值,那么需要在该列上定义此约束,指定在该列上不允许 NULL 值。 NULL 与没有数据是不一样的,它代表着未知的数据。 实例 例如,下面的 SQLite 语句创建一个新的表 COMPANY,并增加了五列,其中 ID、NAME 和 AGE 三列指定不接受 NULL 值:
1 2 3 4 5 6 7 |
<span class="pln">CREATE TABLE COMPANY</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> NAME TEXT NOT NULL</span><span class="pun">,</span><span class="pln"> AGE INT NOT NULL</span><span class="pun">,</span><span class="pln"> ADDRESS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln"> SALARY REAL </span><span class="pun">);</span> |
DEFAULT 约束 DEFAULT 约束在 INSERT INTO 语句没有提供一个特定的值时,为列提供一个默认值。 实例 例如,下面的 SQLite 语句创建一个新的表 COMPANY,并增加了五列。在这里,SALARY 列默认设置为 5000.00。所以当 INSERT INTO 语句没有为该列提供值时,该列将被设置为 5000.00。
1 2 3 4 5 6 7 |
<span class="pln">CREATE TABLE COMPANY</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> NAME TEXT NOT NULL</span><span class="pun">,</span><span class="pln"> AGE INT NOT NULL</span><span class="pun">,</span><span class="pln"> ADDRESS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln"> SALARY REAL DEFAULT </span><span class="lit">50000.00</span> <span class="pun">);</span> |
UNIQUE 约束 UNIQUE 约束防止在一个特定的列存在两个记录具有相同的值。在 COMPANY 表中,例如,您可能要防止两个或两个以上的人具有相同的年龄。 实例 例如,下面的 SQLite 语句创建一个新的表 COMPANY,并增加了五列。在这里,AGE 列设置为 UNIQUE,所以不能有两个相同年龄的记录:
1 2 3 4 5 6 7 |
<span class="pln">CREATE TABLE COMPANY</span><span class="pun">(</span><span class="pln"> ID INT PRIMARY KEY NOT NULL</span><span class="pun">,</span><span class="pln"> NAME TEXT NOT NULL</span><span class="pun">,</span><span class="pln"> AGE INT NOT NULL UNIQUE</span><span class="pun">,</span><span class="pln"> ADDRESS CHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln"> SALARY REAL DEFAULT </span><span class="lit">50000.00</span> <span class="pun">);</span> |
PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每个记录。在一个表中可以有多个 UNIQUE 列,但只能有一个主键。在设计数据库表时,主键是很重要的。主键是唯一的 ID。 我们使用主键来引用表中的行。可通过把主键设置为其他表的外键,来创建表之间的关系。由于"长期存在编码监督",在 SQLite […]
View DetailsSQLite 的 DISTINCT 关键字与 SELECT 语句一起使用,来消除所有重复的记录,并只获取唯一一次记录。 有可能出现一种情况,在一个表中有多个重复的记录。当提取这样的记录时,DISTINCT 关键字就显得特别有意义,它只获取唯一一次记录,而不是获取重复记录。 语法 用于消除重复记录的 DISTINCT 关键字的基本语法如下:
1 2 3 |
<span class="pln">SELECT DISTINCT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,.....</span><span class="pln">columnN FROM table_name WHERE </span><span class="pun">[</span><span class="pln">condition</span><span class="pun">]</span> |
实例 假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> <span class="lit">8</span> <span class="typ">Paul</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">20000.0</span> <span class="lit">9</span> <span class="typ">James</span> <span class="lit">44</span> <span class="typ">Norway</span> <span class="lit">5000.0</span> <span class="lit">10</span> <span class="typ">James</span> <span class="lit">45</span> <span class="typ">Texas</span> <span class="lit">5000.0</span> |
首先,让我们来看看下面的 SELECT 查询,它将返回重复的工资记录:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT name FROM COMPANY</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="pln">NAME </span><span class="pun">----------</span> <span class="typ">Paul</span> <span class="typ">Allen</span> <span class="typ">Teddy</span> <span class="typ">Mark</span> <span class="typ">David</span> <span class="typ">Kim</span> <span class="typ">James</span> <span class="typ">Paul</span> <span class="typ">James</span> <span class="typ">James</span> |
现在,让我们在上述的 SELECT 查询中使用 DISTINCT 关键字:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT DISTINCT name FROM COMPANY</span><span class="pun">;</span> |
这将产生以下结果,没有任何重复的条目:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="pln">NAME </span><span class="pun">----------</span> <span class="typ">Paul</span> <span class="typ">Allen</span> <span class="typ">Teddy</span> <span class="typ">Mark</span> <span class="typ">David</span> <span class="typ">Kim</span> <span class="typ">James from:<a href="http://www.runoob.com/sqlite/sqlite-distinct-keyword.html">http://www.runoob.com/sqlite/sqlite-distinct-keyword.html</a></span> |
HAVING 子句允许指定条件来过滤将出现在最终结果中的分组结果。 WHERE 子句在所选列上设置条件,而 HAVING 子句则在由 GROUP BY 子句创建的分组上设置条件。 语法 下面是 HAVING 子句在 SELECT 查询中的位置:
1 2 3 4 5 6 |
<span class="pln">SELECT FROM WHERE GROUP BY HAVING ORDER BY</span> |
在一个查询中,HAVING 子句必须放在 GROUP BY 子句之后,必须放在 ORDER BY 子句之前。下面是包含 HAVING 子句的 SELECT 语句的语法:
1 2 3 4 5 6 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2 FROM table1</span><span class="pun">,</span><span class="pln"> table2 WHERE </span><span class="pun">[</span><span class="pln"> conditions </span><span class="pun">]</span><span class="pln"> GROUP BY column1</span><span class="pun">,</span><span class="pln"> column2 HAVING </span><span class="pun">[</span><span class="pln"> conditions </span><span class="pun">]</span><span class="pln"> ORDER BY column1</span><span class="pun">,</span><span class="pln"> column2</span> |
实例 假设 COMPANY 表有以下记录:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">1</span> <span class="typ">Paul</span> <span class="lit">32</span> <span class="typ">California</span> <span class="lit">20000.0</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000.0</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000.0</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000.0</span> <span class="lit">7</span> <span class="typ">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> <span class="lit">8</span> <span class="typ">Paul</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">20000.0</span> <span class="lit">9</span> <span class="typ">James</span> <span class="lit">44</span> <span class="typ">Norway</span> <span class="lit">5000.0</span> <span class="lit">10</span> <span class="typ">James</span> <span class="lit">45</span> <span class="typ">Texas</span> <span class="lit">5000.0</span> |
下面是一个实例,它将显示名称计数小于 2 的所有记录:
1 |
<span class="pln">sqlite </span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY GROUP BY name HAVING count</span><span class="pun">(</span><span class="pln">name</span><span class="pun">)</span> <span class="pun"><</span> <span class="lit">2</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000</span> <span class="lit">5</span> <span class="typ">David</span> <span class="lit">27</span> <span class="typ">Texas</span> <span class="lit">85000</span> <span class="lit">6</span> <span class="typ">Kim</span> <span class="lit">22</span> <span class="typ">South</span><span class="pun">-</span><span class="typ">Hall</span> <span class="lit">45000</span> <span class="lit">4</span> <span class="typ">Mark</span> <span class="lit">25</span> <span class="typ">Rich</span><span class="pun">-</span><span class="typ">Mond</span> <span class="lit">65000</span> <span class="lit">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000</span> |
下面是一个实例,它将显示名称计数大于 2 的所有记录:
1 |
<span class="pln">sqlite </span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY GROUP BY name HAVING count</span><span class="pun">(</span><span class="pln">name</span><span class="pun">)</span> <span class="pun">></span> <span class="lit">2</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 |
<span class="pln">ID NAME AGE ADDRESS SALARY </span><span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="pun">----------</span> <span class="lit">10</span> <span class="typ">James</span> <span class="lit">45</span> <span class="typ">Texas</span> <span class="lit">5000 from:<a href="http://www.runoob.com/sqlite/sqlite-having-clause.html">http://www.runoob.com/sqlite/sqlite-having-clause.html</a></span> |