SQLite 的 AND 和 OR 运算符用于编译多个条件来缩小在 SQLite 语句中所选的数据。这两个运算符被称为连接运算符。
这些运算符为同一个 SQLite 语句中不同的运算符之间的多个比较提供了可能。
AND 运算符允许在一个 SQL 语句的 WHERE 子句中的多个条件的存在。使用 AND 运算符时,只有当所有条件都为真(true)时,整个条件为真(true)。例如,只有当 condition1 和 condition2 都为真(true)时,[condition1] AND [condition2] 为真(true)。
带有 WHERE 子句的 AND 运算符的基本语法如下:
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 WHERE </span><span class="pun">[</span><span class="pln">condition1</span><span class="pun">]</span><span class="pln"> AND </span><span class="pun">[</span><span class="pln">condition2</span><span class="pun">]...</span><span class="pln">AND </span><span class="pun">[</span><span class="pln">conditionN</span><span class="pun">];</span> |
您可以使用 AND 运算符来结合 N 个数量的条件。SQLite 语句需要执行的动作是,无论是事务或查询,所有由 AND 分隔的条件都必须为真(TRUE)。
假设 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> |
下面的 SELECT 语句列出了 AGE 大于等于 25 且工资大于等于 65000.00 的所有记录:
1 2 3 4 5 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE AGE </span><span class="pun">>=</span> <span class="lit">25</span><span class="pln"> AND SALARY </span><span class="pun">>=</span> <span class="lit">65000</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">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> |
OR 运算符也用于结合一个 SQL 语句的 WHERE 子句中的多个条件。使用 OR 运算符时,只要当条件中任何一个为真(true)时,整个条件为真(true)。例如,只要当 condition1 或 condition2 有一个为真(true)时,[condition1] OR [condition2] 为真(true)。
带有 WHERE 子句的 OR 运算符的基本语法如下:
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 WHERE </span><span class="pun">[</span><span class="pln">condition1</span><span class="pun">]</span><span class="pln"> OR </span><span class="pun">[</span><span class="pln">condition2</span><span class="pun">]...</span><span class="pln">OR </span><span class="pun">[</span><span class="pln">conditionN</span><span class="pun">]</span> |
您可以使用 OR 运算符来结合 N 个数量的条件。SQLite 语句需要执行的动作是,无论是事务或查询,只要任何一个由 OR 分隔的条件为真(TRUE)即可。
假设 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> |
下面的 SELECT 语句列出了 AGE 大于等于 25 或工资大于等于 65000.00 的所有记录:
1 2 3 4 5 6 7 8 9 10 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE AGE </span><span class="pun">>=</span> <span class="lit">25</span><span class="pln"> OR SALARY </span><span class="pun">>=</span> <span class="lit">65000</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">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-and-or-clauses.html">http://www.runoob.com/sqlite/sqlite-and-or-clauses.html</a></span> |