mysql與oracle的分組函式
mysql的group by與oracle有區別。
在oracle中,分組列以外的列若出現在最終查詢結果中,必須要加函式,不能直接輸出結果列。
但在mysql中可以直接輸出,其結果是按分組列順序找到的第一條記錄,後面的將被忽略。
如:
mysql> create table te(a varchar(2),b varchar(4));
Query OK, 0 rows affected (0.13 sec)
在oracle中,分組列以外的列若出現在最終查詢結果中,必須要加函式,不能直接輸出結果列。
但在mysql中可以直接輸出,其結果是按分組列順序找到的第一條記錄,後面的將被忽略。
如:
mysql> create table te(a varchar(2),b varchar(4));
Query OK, 0 rows affected (0.13 sec)
mysql> insert into te values('ab','abc');
Query OK, 1 row affected (0.09 sec)
Query OK, 1 row affected (0.09 sec)
mysql> insert into te values('ab','bcd');
Query OK, 1 row affected (0.06 sec)
Query OK, 1 row affected (0.06 sec)
mysql> insert into te values('ab','def');
Query OK, 1 row affected (0.09 sec)
Query OK, 1 row affected (0.09 sec)
mysql> insert into te values('ac','def');
Query OK, 1 row affected (0.08 sec)
Query OK, 1 row affected (0.08 sec)
mysql> insert into te values('ac','eef');
Query OK, 1 row affected (0.08 sec)
Query OK, 1 row affected (0.08 sec)
mysql> select * from te;
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | def |
| ac | eef |
+------+------+
5 rows in set (0.00 sec)
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | def |
| ac | eef |
+------+------+
5 rows in set (0.00 sec)
mysql> select * from te group by a;
+------+------+
| a | b |
+------+------+
| ab | abc |
| ac | def |
+------+------+
2 rows in set (0.00 sec)
+------+------+
| a | b |
+------+------+
| ab | abc |
| ac | def |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from te group by a;
+------+------+
| a | b |
+------+------+
| ab | abc |
| ac | def |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from te group by a;
+------+------+
| a | b |
+------+------+
| ab | abc |
| ac | def |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from te group by b;
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | eef |
+------+------+
4 rows in set (0.00 sec)
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | eef |
+------+------+
4 rows in set (0.00 sec)
mysql> select * from te group by b;
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | eef |
+------+------+
4 rows in set (0.00 sec)
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | eef |
+------+------+
4 rows in set (0.00 sec)
========================================================
Connected to Oracle Database 10g Release 10.2.0.1.0
Connected as scott
Connected as scott
SQL> create table te(a varchar(2),b varchar(4));
Table created
SQL> insert into te values('ab','abc');
1 row inserted
SQL> insert into te values('ab','bcd');
1 row inserted
SQL> insert into te values('ab','def');
1 row inserted
SQL>
SQL> insert into te values('ac','def');
SQL> insert into te values('ac','def');
1 row inserted
SQL> insert into te values('ac','eef');
1 row inserted
SQL> select * from te;
A B
-- ----
ab abc
ab bcd
ab def
ac def
ac eef
-- ----
ab abc
ab bcd
ab def
ac def
ac eef
SQL> select * from te group by a;
select * from te group by a
ORA-00979: 不是 GROUP BY 表示式
SQL> rollback;
Rollback complete
========================================================
另外,mysql中一條命令可以包含多個語句,而oracle不行:
mysql> select * from te;select * from te;
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | def |
| ac | eef |
+------+------+
5 rows in set (0.00 sec)
mysql> select * from te;select * from te;
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | def |
| ac | eef |
+------+------+
5 rows in set (0.00 sec)
+------+------+
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | def |
| ac | eef |
+------+------+
5 rows in set (0.00 sec)
| a | b |
+------+------+
| ab | abc |
| ab | bcd |
| ab | def |
| ac | def |
| ac | eef |
+------+------+
5 rows in set (0.00 sec)
SQL> select * from te;select * from te;
select * from te;select * from te
ORA-00911: 無效字元
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26451536/viewspace-766951/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL之集合函式與分組查詢MySql函式
- 6、Oracle中的分組函式Oracle函式
- MySQL資料庫中的分組函式ROLLUPMySql資料庫函式
- 【ROLLUP】Oracle分組函式之ROLLUP魅力Oracle函式
- 【CUBE】Oracle分組函式之CUBE魅力Oracle函式
- 關於Oracle自定義分組函式Oracle函式
- Oracle 分組彙總統計函式的使用Oracle函式
- oracle 與 mysql 中的函式總結OracleMySql函式
- Mysql中常用函式 分組,連線查詢MySql函式
- group by分組函式之rollup與cube用法函式
- MySQL8.0-分組函式ROLLUP的基本用法(GROUPING)MySql函式
- ORACLE單行函式與多行函式之七:多行函式之分組函式示例Oracle函式
- MySQL全面瓦解10:分組查詢和聚合函式MySql函式
- mysql和oracle字串編碼轉換函式,字串轉位元組函式例子MySqlOracle字串編碼函式
- MySQL - 分組連線欄位函式GROUP_CONCAT的使用MySql函式
- 聚合函式及分組與過濾(GROUP BY … HAVING)函式
- Oracle - 分組連線欄位函式WMSYS.WM_CONCAT的使用Oracle函式
- 【函式】Oracle EXTRACT()函式與to_char() 函式函式Oracle
- MySQL教程之分組函式(五)MySql函式
- mysql count函式與分頁功能極限優化MySql函式優化
- Oracle和MySQL分組查詢GROUP BYOracleMySql
- Oracle分析函式與視窗函式Oracle函式
- oracle函式與操作Oracle函式
- mysql count函式與分頁功能極限最佳化MySql函式
- MySQL的Group By分組MySql
- python 系統函式呼叫sed分組Python函式
- 資料庫之DQL排序&分組&函式資料庫排序函式
- MYSQL——分組MySql
- MySQL CAST與CONVERT 函式的用法MySqlAST函式
- 【Mysql】Mysql似oracle分析函式sum over的實現MySqlOracle函式
- Oracle分組查詢中包含子查詢列,發生ORA-00937:不是單分組函式的錯誤Oracle函式
- Mysql與Sql Server DATEDIFF函式MySqlServer函式
- MySQL學習記錄--分組與聚集MySql
- ORACLE單行函式與多行函式之一Oracle函式
- Sql字串分組Split函式的兩種實現方法SQL字串函式
- oracle下資料的排序分組row_number() over()--分析函式,可用於去重Oracle排序函式
- ORACLE單行函式與多行函式之二:字元函式示例Oracle函式字元
- ORACLE單行函式與多行函式之三:數值函式Oracle函式