SQLite 的 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> |