SQLite Where 子句


sqlite的 where 子句用于指定从一个表或多个表中获取数据的条件。

如果满足给定的条件,即为真(true)时,则从表中返回特定的值。您可以使用 where 子句来过滤记录,只获取需要的记录。

where 子句不仅可用在 select 语句中,它也可用在 update、delete 语句中,等等,这些我们将在随后的章节中学习到。

 

1. 语法

sqlite 的带有 where 子句的 select 语句的基本语法如下:

select column1, column2, columnn 
from table_name
where [condition]

 

2. 范例

您还可以使用比较或逻辑运算符指定条件,比如 >、<、=、like、not,等等。假设 company 表有以下记录:

id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
1           paul        32          california  20000.0
2           allen       25          texas       15000.0
3           teddy       23          norway      20000.0
4           mark        25          rich-mond   65000.0
5           david       27          texas       85000.0
6           kim         22          south-hall  45000.0
7           james       24          houston     10000.0

下面的范例演示了 sqlite 逻辑运算符的用法。下面的 select 语句列出了 age 大于等于 25 工资大于等于 65000.00 的所有记录:

sqlite> select * from company where age >= 25 and salary >= 65000;
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
4           mark        25          rich-mond   65000.0
5           david       27          texas       85000.0

下面的 select 语句列出了 age 大于等于 25 工资大于等于 65000.00 的所有记录:

sqlite> select * from company where age >= 25 or salary >= 65000;
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
1           paul        32          california  20000.0
2           allen       25          texas       15000.0
4           mark        25          rich-mond   65000.0
5           david       27          texas       85000.0

下面的 select 语句列出了 age 不为 null 的所有记录,结果显示所有的记录,意味着没有一个记录的 age 等于 null:

sqlite>  select * from company where age is not null;
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
1           paul        32          california  20000.0
2           allen       25          texas       15000.0
3           teddy       23          norway      20000.0
4           mark        25          rich-mond   65000.0
5           david       27          texas       85000.0
6           kim         22          south-hall  45000.0
7           james       24          houston     10000.0

下面的 select 语句列出了 name 以 'ki' 开始的所有记录,'ki' 之后的字符不做限制:

sqlite> select * from company where name like 'ki%';
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
6           kim         22          south-hall  45000.0

下面的 select 语句列出了 name 以 'ki' 开始的所有记录,'ki' 之后的字符不做限制:

sqlite> select * from company where name glob 'ki*';
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
6           kim         22          south-hall  45000.0

下面的 select 语句列出了 age 的值为 25 或 27 的所有记录:

sqlite> select * from company where age in ( 25, 27 );
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
2           allen       25          texas       15000.0
4           mark        25          rich-mond   65000.0
5           david       27          texas       85000.0

下面的 select 语句列出了 age 的值既不是 25 也不是 27 的所有记录:

sqlite> select * from company where age not in ( 25, 27 );
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
1           paul        32          california  20000.0
3           teddy       23          norway      20000.0
6           kim         22          south-hall  45000.0
7           james       24          houston     10000.0

下面的 select 语句列出了 age 的值在 25 与 27 之间的所有记录:

sqlite> select * from company where age between 25 and 27;
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
2           allen       25          texas       15000.0
4           mark        25          rich-mond   65000.0
5           david       27          texas       85000.0

下面的 select 语句使用 sql 子查询,子查询查找 salary > 65000 的带有 age 字段的所有记录,后边的 where 子句与 exists 运算符一起使用,列出了外查询中的 age 存在于子查询返回的结果中的所有记录:

sqlite> select age from company 
        where exists (select age from company where salary > 65000);
age
----------
32
25
23
25
27
22
24

下面的 select 语句使用 sql 子查询,子查询查找 salary > 65000 的带有 age 字段的所有记录,后边的 where 子句与 > 运算符一起使用,列出了外查询中的 age 大于子查询返回的结果中的年龄的所有记录:

sqlite> select * from company 
        where age > (select age from company where salary > 65000);
id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
1           paul        32          california  20000.0

下一节:sqlite and/or 运算符

sqlite教程

相关文章