?🐓?序言
StarRocks 是新一代極速全場景 MPP (Massively Parallel Processing) 數據庫。StarRocks 的愿景是能夠讓用戶的數據分析變得更加簡單和敏捷。用戶無需經過復雜的預處理,可以用StarRocks 來支持多種數據分析場景的極速分析。
?🐓?語法區別
字符串操作函數(String Functions)
CONCAT_WS
MySQL: CONCAT_WS用于連接字符串,并可指定分隔符。
StarRocks: 不支持CONCAT_WS函數,但可以通過使用concat()和join()方法來實現相同效果。
示例:
CONCAT()
將多個字符串連接起來。如果參數中任意一個值是 NULL,那么返回的結果為 NULL。
MySQL > select concat("a", "b");
+------------------+
| concat('a', 'b') |
+------------------+
| ab |
+------------------+MySQL > select concat("a", "b", "c");
+-----------------------+
| concat('a', 'b', 'c') |
+-----------------------+
| abc |
+-----------------------+MySQL > select concat("a", null, "c");
+------------------------+
| concat('a', NULL, 'c') |
+------------------------+
| NULL |
+------------------------+
SUBSTRING_INDEX()
MySQL: 返回字符串中指定分隔符出現的第n個實例之前或之后的所有字符。
Starrocks: 不支持SUBSTRING_INDEX()函數,可以使用substring_index替代。?
示例:
Substring_Index()
-- 從左往右數截取第二個 `.` 分隔符前面的字符串。
mysql> select substring_index('https://www.starrocks.io', '.', 2);
+-----------------------------------------------------+
| substring_index('https://www.starrocks.io', '.', 2) |
+-----------------------------------------------------+
| https://www.starrocks |
+-----------------------------------------------------+-- Count 為負,從右往左數截取第二個 `.` 分隔符之后的字符串,
mysql> select substring_index('https://www.starrocks.io', '.', -2);
+------------------------------------------------------+
| substring_index('https://www.starrocks.io', '.', -2) |
+------------------------------------------------------+
| starrocks.io |
+------------------------------------------------------+mysql> select substring_index("hello world", " ", 1);
+----------------------------------------+
| substring_index("hello world", " ", 1) |
+----------------------------------------+
| hello |
+----------------------------------------+mysql> select substring_index("hello world", " ", -1);
+-----------------------------------------+
| substring_index('hello world', ' ', -1) |
+-----------------------------------------+
| world |
+-----------------------------------------+-- Count 為 0,返回 NULL。
mysql> select substring_index("hello world", " ", 0);
+----------------------------------------+
| substring_index('hello world', ' ', 0) |
+----------------------------------------+
| NULL |
+----------------------------------------+-- Count 大于 `delimiter` 實際出現的次數,返回整個字符串。
mysql> select substring_index("hello world", " ", 2);
+----------------------------------------+
| substring_index("hello world", " ", 2) |
+----------------------------------------+
| hello world |
+----------------------------------------+-- Count 大于 `delimiter` 實際出現的次數,返回整個字符串。
mysql> select substring_index("hello world", " ", -2);
+-----------------------------------------+
| substring_index("hello world", " ", -2) |
+-----------------------------------------+
| hello world |
+-----------------------------------------+
LENGTH()
MySQL: 返回字符串長度。
Starrocks:同樣支持LENGTH()函數。
?示例:
LENGTH()
MySQL > select length("abc");
+---------------+
| length('abc') |
+---------------+
| 3 |
+---------------+MySQL > select length("中國");
+------------------+
| length('中國') |
+------------------+
| 6 |
+------------------+
時間日期處理函數(Date and Time Functions)
YEARWEEK()
MySQL: 返回帶有年份和周數組成的值。
Starrocks: 并不直接支持YEARWEEK()函數,但可以通過DATE_FORMAT(date, ‘%Y%u’)來達到類似效果
示例 :
DATE_FORMAT()
select date_format('2009-10-04 22:23:00', '%W %M %Y');
+------------------------------------------------+
| date_format('2009-10-04 22:23:00', '%W %M %Y') |
+------------------------------------------------+
| Sunday October 2009 |
+------------------------------------------------+select date_format('2007-10-04 22:23:00', '%H:%i:%s');
+------------------------------------------------+
| date_format('2007-10-04 22:23:00', '%H:%i:%s') |
+------------------------------------------------+
| 22:23:00 |
+------------------------------------------------+select date_format('1900-10-04 22:23:00', '%D %y %a %d %m %b %j');
+------------------------------------------------------------+
| date_format('1900-10-04 22:23:00', '%D %y %a %d %m %b %j') |
+------------------------------------------------------------+
| 4th 00 Thu 04 10 Oct 277 |
+------------------------------------------------------------+select date_format('1997-10-04 22:23:00', '%H %k %I %r %T %S %w');
+------------------------------------------------------------+
| date_format('1997-10-04 22:23:00', '%H %k %I %r %T %S %w') |
+------------------------------------------------------------+
| 22 22 10 10:23:00 PM 22:23:00 00 6 |
+------------------------------------------------------------+select date_format('1999-01-01 00:00:00', '%X %V');
+---------------------------------------------+
| date_format('1999-01-01 00:00:00', '%X %V') |
+---------------------------------------------+
| 1998 52 |
+---------------------------------------------+select date_format('2006-06-01', '%d');
+------------------------------------------+
| date_format('2006-06-01 00:00:00', '%d') |
+------------------------------------------+
| 01 |
+------------------------------------------+select date_format('2006-06-01', '%%%d');
+--------------------------------------------+
| date_format('2006-06-01 00:00:00', '%%%d') |
+--------------------------------------------+
| %01 |
+--------------------------------------------+
聚合函數(Aggregate Functions)?
COUNT(DISTINCT)
MySQL: 可以使用COUNT(DISTINCT)來計算唯一值的數量。
Starrocks: 目前并不支持COUNT(DISTINCT)函數。
SUM() 和 AVG()????????
MySQL: 分別用于求和和平均值。
Starrocks: 同樣支持SUM()和AVG()函數。
示例:
SUM()
1.創建表
CREATE TABLE IF NOT EXISTS employees (region_num TINYINT COMMENT "range [-128, 127]",id BIGINT COMMENT "range [-2^63 + 1 ~ 2^63 - 1]",hobby STRING NOT NULL COMMENT "upper limit value 65533 bytes",income DOUBLE COMMENT "8 bytes",sales DECIMAL(12,4) COMMENT "")DISTRIBUTED BY HASH(region_num);
2.插入數據?
INSERT INTO employees VALUES
(3,432175,'3',25600,1250.23),
(4,567832,'3',37932,2564.33),
(3,777326,'2',null,1932.99),
(5,342611,'6',43727,45235.1),
(2,403882,'4',36789,52872.4);
3.求和
MySQL > SELECT region_num, sum(sales) from employees
group by region_num;+------------+------------+
| region_num | sum(sales) |
+------------+------------+
| 2 | 52872.4000 |
| 5 | 45235.1000 |
| 4 | 2564.3300 |
| 3 | 3183.2200 |
+------------+------------+
4 rows in set (0.01 sec)
AVG()同Mysql一樣?
GROUP_CONCAT()
MySQL: 可以使用GROUP_CONCAT來將多行數據拼接成一個字符串。
Starrocks: 目前并不支持GROUP_CONCAT函數。
GROUP BY ()
MySQL: 支持對結果集進行分組,并可以在SELECT子句中使用非聚合列。
Starrocks: 在SELECT子句中只能使用聚合列或者通過HAVING子句過濾后才能引用非聚合列。