当前位置:首页 » 《随便一记》 » 正文

Mysql与StarRocks语法上的不同

26 人参与  2024年03月03日 09:17  分类 : 《随便一记》  评论

点击全文阅读


 ? 序言

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 employeesgroup 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子句过滤后才能引用非聚合列。


点击全文阅读


本文链接:http://zhangshiyu.com/post/72956.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1