SQLite的 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> |
SQLite 的 GROUP BY 子句用于与 SELECT 语句一起使用,来对相同的数据进行分组。 在 SELECT 语句中,GROUP BY 子句放在 WHERE 子句之后,放在 ORDER BY 子句之前。 语法 下面给出了 GROUP BY 子句的基本语法。GROUP BY 子句必须放在 WHERE 子句中的条件之后,必须放在 ORDER BY 子句之前。
1 2 3 4 5 |
<span class="pln">SELECT column</span><span class="pun">-</span><span class="pln">list FROM table_name 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</span><span class="pun">....</span><span class="pln">columnN ORDER BY column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN</span> |
您可以在 GROUP BY 子句中使用多个列。确保您使用的分组列在列清单中。 实例 假设 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> |
如果您想了解每个客户的工资总额,则可使用 GROUP BY 查询,如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT NAME</span><span class="pun">,</span><span class="pln"> SUM</span><span class="pun">(</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> FROM COMPANY GROUP BY NAME</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">NAME SUM</span><span class="pun">(</span><span class="pln">SALARY</span><span class="pun">)</span> <span class="pun">----------</span> <span class="pun">-----------</span> <span class="typ">Allen</span> <span class="lit">15000.0</span> <span class="typ">David</span> <span class="lit">85000.0</span> <span class="typ">James</span> <span class="lit">10000.0</span> <span class="typ">Kim</span> <span class="lit">45000.0</span> <span class="typ">Mark</span> <span class="lit">65000.0</span> <span class="typ">Paul</span> <span class="lit">20000.0</span> <span class="typ">Teddy</span> <span class="lit">20000.0</span> |
现在,让我们使用下面的 INSERT 语句在 COMPANY 表中另外创建三个记录:
1 2 3 |
<span class="pln">INSERT INTO COMPANY VALUES </span><span class="pun">(</span><span class="lit">8</span><span class="pun">,</span> <span class="str">'Paul'</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">20000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY VALUES </span><span class="pun">(</span><span class="lit">9</span><span class="pun">,</span> <span class="str">'James'</span><span class="pun">,</span> <span class="lit">44</span><span class="pun">,</span> <span class="str">'Norway'</span><span class="pun">,</span> <span class="lit">5000.00</span> <span class="pun">);</span><span class="pln"> INSERT INTO COMPANY VALUES </span><span class="pun">(</span><span class="lit">10</span><span class="pun">,</span> <span class="str">'James'</span><span class="pun">,</span> <span class="lit">45</span><span class="pun">,</span> <span class="str">'Texas'</span><span class="pun">,</span> <span class="lit">5000.00</span> <span class="pun">);</span> |
现在,我们的表具有重复名称的记录,如下所示:
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> |
让我们用同样的 GROUP BY 语句来对所有记录按 NAME 列进行分组,如下所示:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT NAME</span><span class="pun">,</span><span class="pln"> SUM</span><span class="pun">(</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> FROM COMPANY GROUP BY NAME ORDER BY NAME</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">NAME SUM</span><span class="pun">(</span><span class="pln">SALARY</span><span class="pun">)</span> <span class="pun">----------</span> <span class="pun">-----------</span> <span class="typ">Allen</span> <span class="lit">15000</span> <span class="typ">David</span> <span class="lit">85000</span> <span class="typ">James</span> <span class="lit">20000</span> <span class="typ">Kim</span> <span class="lit">45000</span> <span class="typ">Mark</span> <span class="lit">65000</span> <span class="typ">Paul</span> <span class="lit">40000</span> <span class="typ">Teddy</span> <span class="lit">20000</span> |
让我们把 ORDER BY 子句与 GROUP BY 子句一起使用,如下所示:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT NAME</span><span class="pun">,</span><span class="pln"> SUM</span><span class="pun">(</span><span class="pln">SALARY</span><span class="pun">)</span><span class="pln"> FROM COMPANY GROUP BY NAME ORDER BY NAME DESC</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 9 |
<span class="pln">NAME SUM</span><span class="pun">(</span><span class="pln">SALARY</span><span class="pun">)</span> <span class="pun">----------</span> <span class="pun">-----------</span> <span class="typ">Teddy</span> <span class="lit">20000</span> <span class="typ">Paul</span> <span class="lit">40000</span> <span class="typ">Mark</span> <span class="lit">65000</span> <span class="typ">Kim</span> <span class="lit">45000</span> <span class="typ">James</span> <span class="lit">20000</span> <span class="typ">David</span> <span class="lit">85000</span> <span class="typ">Allen</span> <span class="lit">15000</span> |
from:http://www.runoob.com/sqlite/sqlite-group-by.html
View DetailsSQLite 的 ORDER BY 子句是用来基于一个或多个列按升序或降序顺序排列数据。 语法 ORDER BY 子句的基本语法如下:
1 2 3 4 |
<span class="pln">SELECT column</span><span class="pun">-</span><span class="pln">list FROM table_name </span><span class="pun">[</span><span class="pln">WHERE condition</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">ORDER BY column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span> <span class="pun">..</span><span class="pln"> columnN</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">ASC </span><span class="pun">|</span><span class="pln"> DESC</span><span class="pun">];</span> |
您可以在 ORDER BY 子句中使用多个列。确保您使用的排序列在列清单中。 实例 假设 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> |
下面是一个实例,它会将结果按 SALARY 升序排序:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY ORDER BY SALARY ASC</span><span class="pun">;</span> |
这将产生以下结果:
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">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">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">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">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">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">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> |
下面是一个实例,它会将结果按 NAME 和 SALARY 升序排序:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY ORDER BY NAME</span><span class="pun">,</span><span class="pln"> SALARY ASC</span><span class="pun">;</span> |
这将产生以下结果:
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">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">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">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">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">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">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">3</span> <span class="typ">Teddy</span> <span class="lit">23</span> <span class="typ">Norway</span> <span class="lit">20000.0</span> |
下面是一个实例,它会将结果按 NAME 降序排序:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY ORDER BY NAME DESC</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 9 10 11 |
<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">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">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">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">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">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">2</span> <span class="typ">Allen</span> <span class="lit">25</span> <span class="typ">Texas</span> <span class="lit">15000.0 from:<a href="http://www.runoob.com/sqlite/sqlite-order-by.html">http://www.runoob.com/sqlite/sqlite-order-by.html</a></span> |
SQLite 的 LIMIT 子句用于限制由 SELECT 语句返回的数据数量。 语法 带有 LIMIT 子句的 SELECT 语句的基本语法如下:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name LIMIT </span><span class="pun">[</span><span class="kwd">no</span><span class="pln"> of rows</span><span class="pun">]</span> |
下面是 LIMIT 子句与 OFFSET 子句一起使用时的语法:
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,</span><span class="pln"> columnN FROM table_name LIMIT </span><span class="pun">[</span><span class="kwd">no</span><span class="pln"> of rows</span><span class="pun">]</span><span class="pln"> OFFSET </span><span class="pun">[</span><span class="pln">row num</span><span class="pun">]</span> |
SQLite 引擎将返回从下一行开始直到给定的 OFFSET 为止的所有行,如下面的最后一个实例所示。 实例 假设 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> |
下面是一个实例,它限制了您想要从表中提取的行数:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY LIMIT </span><span class="lit">6</span><span class="pun">;</span> |
这将产生以下结果:
1 2 3 4 5 6 7 8 |
<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> |
但是,在某些情况下,可能需要从一个特定的偏移开始提取记录。下面是一个实例,从第三位开始提取 3 个记录:
1 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY LIMIT </span><span class="lit">3</span><span class="pln"> OFFSET </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">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 from:<a href="http://www.runoob.com/sqlite/sqlite-limit-clause.html">http://www.runoob.com/sqlite/sqlite-limit-clause.html</a></span> |
SQLite 的 GLOB 运算符是用来匹配通配符指定模式的文本值。如果搜索表达式与模式表达式匹配,GLOB 运算符将返回真(true),也就是 1。与 LIKE 运算符不同的是,GLOB 是大小写敏感的,对于下面的通配符,它遵循 UNIX 的语法。 星号 (*) 问号 (?) 星号(*)代表零个、一个或多个数字或字符。问号(?)代表一个单一的数字或字符。这些符号可以被组合使用。 语法 * 和 ? 的基本语法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<span class="pln">SELECT FROM table_name WHERE column GLOB </span><span class="str">'XXXX*'</span> <span class="kwd">or</span><span class="pln"> SELECT FROM table_name WHERE column GLOB </span><span class="str">'*XXXX*'</span> <span class="kwd">or</span><span class="pln"> SELECT FROM table_name WHERE column GLOB </span><span class="str">'XXXX?'</span> <span class="kwd">or</span><span class="pln"> SELECT FROM table_name WHERE column GLOB </span><span class="str">'?XXXX'</span> <span class="kwd">or</span><span class="pln"> SELECT FROM table_name WHERE column GLOB </span><span class="str">'?XXXX?'</span> <span class="kwd">or</span><span class="pln"> SELECT FROM table_name WHERE column GLOB </span><span class="str">'????'</span> |
您可以使用 AND 或 OR 运算符来结合 N 个数量的条件。在这里,XXXX 可以是任何数字或字符串值。 实例 下面一些实例演示了 带有 '*' 和 '?' 运算符的 GLOB 子句不同的地方: 语句 描述 WHERE SALARY GLOB '200*' 查找以 200 开头的任意值 WHERE SALARY GLOB '*200*' 查找任意位置包含 200 的任意值 WHERE SALARY GLOB '?00*' 查找第二位和第三位为 00 的任意值 WHERE SALARY GLOB '2??' 查找以 2 开头,且长度至少为 3 个字符的任意值 WHERE SALARY GLOB '*2' 查找以 2 结尾的任意值 WHERE SALARY GLOB '?2*3' 查找第二位为 2,且以 3 结尾的任意值 WHERE SALARY […]
View DetailsSQLite 的 LIKE 运算符是用来匹配通配符指定模式的文本值。如果搜索表达式与模式表达式匹配,LIKE 运算符将返回真(true),也就是 1。这里有两个通配符与 LIKE 运算符一起使用: 百分号 (%) 下划线 (_) 百分号(%)代表零个、一个或多个数字或字符。下划线(_)代表一个单一的数字或字符。这些符号可以被组合使用。 语法 % 和 _ 的基本语法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<span class="pln">SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'XXXX%'</span> <span class="kwd">or</span><span class="pln"> SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'%XXXX%'</span> <span class="kwd">or</span><span class="pln"> SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'XXXX_'</span> <span class="kwd">or</span><span class="pln"> SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'_XXXX'</span> <span class="kwd">or</span><span class="pln"> SELECT column_list FROM table_name WHERE column LIKE </span><span class="str">'_XXXX_'</span> |
您可以使用 AND 或 OR 运算符来结合 N 个数量的条件。在这里,XXXX 可以是任何数字或字符串值。 实例 下面一些实例演示了 带有 '%' 和 '_' 运算符的 LIKE 子句不同的地方: 语句 描述 WHERE SALARY LIKE '200%' 查找以 200 开头的任意值 WHERE SALARY LIKE '%200%' 查找任意位置包含 200 的任意值 WHERE SALARY LIKE '_00%' 查找第二位和第三位为 00 的任意值 WHERE SALARY LIKE '2_%_%' 查找以 2 开头,且长度至少为 3 个字符的任意值 WHERE SALARY LIKE '%2' 查找以 2 结尾的任意值 WHERE SALARY LIKE '_2%3' 查找第二位为 2,且以 3 结尾的任意值 WHERE SALARY LIKE '2___3' 查找长度为 […]
View Details