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