表达式是一个或多个值、运算符和计算值的SQL函数的组合。
SQL 表达式与公式类似,都写在查询语言中。您还可以使用特定的数据集来查询数据库。
假设 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 WHERE </span><span class="pun">[</span><span class="pln">CONTION </span><span class="pun">|</span><span class="pln"> EXPRESSION</span><span class="pun">];</span> |
有不同类型的 SQLite 表达式,具体讲解如下:
SQLite 的布尔表达式在匹配单个值的基础上获取数据。语法如下:
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 SINGLE VALUE MATCHTING EXPRESSION</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> |
下面的实例演示了 SQLite 布尔表达式的用法:
1 2 3 4 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">*</span><span class="pln"> FROM COMPANY WHERE SALARY </span><span class="pun">=</span> <span class="lit">10000</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">James</span> <span class="lit">24</span> <span class="typ">Houston</span> <span class="lit">10000.0</span> |
这些表达式用来执行查询中的任何数学运算。语法如下:
1 2 |
<span class="pln">SELECT numerical_expression </span><span class="kwd">as</span><span class="pln"> OPERATION_NAME </span><span class="pun">[</span><span class="pln">FROM table_name WHERE CONDITION</span><span class="pun">]</span> <span class="pun">;</span> |
在这里,numerical_expression 用于数学表达式或任何公式。下面的实例演示了 SQLite 数值表达式的用法:
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT </span><span class="pun">(</span><span class="lit">15</span> <span class="pun">+</span> <span class="lit">6</span><span class="pun">)</span><span class="pln"> AS ADDITION ADDITION </span><span class="pun">=</span> <span class="lit">21</span> |
有几个内置的函数,比如 avg()、sum()、count(),等等,执行被称为对一个表或一个特定的表列的汇总数据计算。
1 2 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT COUNT</span><span class="pun">(*)</span><span class="pln"> AS </span><span class="str">"RECORDS"</span><span class="pln"> FROM COMPANY</span><span class="pun">;</span><span class="pln"> RECORDS </span><span class="pun">=</span> <span class="lit">7</span> |
日期表达式返回当前系统日期和时间值,这些表达式将被用于各种数据操作。
1 2 3 4 |
<span class="pln">sqlite</span><span class="pun">></span><span class="pln"> SELECT CURRENT_TIMESTAMP</span><span class="pun">;</span><span class="pln"> CURRENT_TIMESTAMP </span><span class="pun">=</span> <span class="lit">2013</span><span class="pun">-</span><span class="lit">03</span><span class="pun">-</span><span class="lit">17</span> <span class="lit">10</span><span class="pun">:</span><span class="lit">43</span><span class="pun">:</span><span class="lit">35 from:http://www.runoob.com/sqlite/sqlite-expressions.html</span> |