在SELECT子句之前或之后解析GROUP BY子句

时间:2016-06-25 16:45:37

标签: mysql

我正在尝试理解MySQL DBMS中SELECT查询执行的逻辑顺序。

阅读thisthis个问题,订单可以是:

  
      
  1. FROM
  2.   
  3. ON
  4.   
  5. JOIN
  6.   
  7. WHERE
  8.   
  9. GROUP BY
  10.   
  11. WITH CUBE或WITH ROLLUP
  12.   
  13. HAVING
  14.   
  15. 选择
  16.   
  17. DISTINCT
  18.   
  19. ORDER BY
  20.   
  21. TOP
  22.   

或者:

  
      
  1. FROM clause
  2.   
  3. WHERE子句
  4.   
  5. SELECT子句
  6.   
  7. GROUP BY子句
  8.   
  9. HAVING条款
  10.   
  11. ORDER BY子句
  12.   

正如你所看到的那样,两者的顺序相反,所以我开始摆弄MySQL。

表格(PROGETTO)如下:

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| NOME_P   | varchar(15) | NO   | UNI | NULL    |       |
| NUMERO_P | int(11)     | NO   | PRI | NULL    |       |
| SEDE_P   | varchar(15) | YES  |     | NULL    |       |
| NUM_D    | int(11)     | NO   | MUL | NULL    |       |
+----------+-------------+------+-----+---------+-------+

数据集是:

+-----------------+----------+-----------+-------+
| NOME_P          | NUMERO_P | SEDE_P    | NUM_D |
+-----------------+----------+-----------+-------+
| ProdottoX       |        1 | Bellaire  |     5 |
| ProdottoY       |        2 | Sugarland |     5 |
| ProdottoZ       |        3 | Houston   |     5 |
| Informatizzazio |       10 | Stafford  |     4 |
| Riorganizzazion |       20 | Houston   |     1 |
| Nuove opportuni |       30 | Stafford  |     4 |
+-----------------+----------+-----------+-------+

所以,我发起了第一个查询:

mysql> SELECT NOME_P FROM PROGETTO GROUP BY NUM_D;
+-----------------+
| NOME_P          |
+-----------------+
| Riorganizzazion |
| Informatizzazio |
| ProdottoX       |
+-----------------+

我想,“好的顺序必须是第一个,但让我们再试一次......”然后我执行了:

mysql> SELECT NOME_P AS NNN FROM PROGETTO GROUP BY  NNN;
+-----------------+
| NNN             |
+-----------------+
| Informatizzazio |
| Nuove opportuni |
| ProdottoX       |
| ProdottoY       |
| ProdottoZ       |
| Riorganizzazion |
+-----------------+

......这让我很困惑。

0 个答案:

没有答案