MySQL ClickHouse不同于SQL的语法介绍

下面只展示和MySQL或HIVE区别较大的语法

例如ClickHouse建库语法和MySQL几乎1卵样的不予展示

[var]

# 创建左表
CREATE TABLE default.temp_left(
`a` String COMMENT ‘匹配键’
)ENGINE = MergeTree
ORDER BY (a);
INSERT INTO default.temp_left
SELECT ‘A1’ AS a UNION ALL
SELECT ‘A1’ AS a UNION ALL
SELECT ‘A2’ AS a UNION ALL
SELECT ‘A3’ AS a;
# 创建右表
CREATE TABLE default.temp_right(
`a` String COMMENT ‘匹配键’,
`b` Nullable(UInt32),
`c` UInt32
)ENGINE = MergeTree
ORDER BY (a);
INSERT INTO default.temp_right
SELECT ‘A2’ AS a,9 AS b,8 AS c UNION ALL
SELECT ‘A3’ AS a,9 AS b,8 AS c UNION ALL
SELECT ‘A4’ AS a,9 AS b,8 AS c;
# 左联
SELECT *
FROM default.temp_left le
LEFT JOIN default.temp_right ri ON le.a=ri.a

左联测试结果

在右表中,b允许空,ac冇允许空 左联后,联不上的a是空字符串,联不上的bNULL,联不上的c是0

[var]

INSERT INTO t2
WITH a AS (SELECT * FROM t1)
SELECT * FROM a;

和HIVE、MySQL等不一样,ClickHouse的INSERT写在WITH之前

[var]

  • 当回话结束时,临时表将随会话一起消失
  • 临时表仅能用Memory表引擎
  • 无法为临时表指定数据库,它是在数据库之外创建的
  • 当查询没有指定库,且临时表与另一个表名相同 时,会优先使用临时表
CREATE TEMPORARY TABLE temp_t(`a` String,`b` Int32);
INSERT INTO temp_t VALUES (‘AB’,3),(‘CC’,4);
SELECT * FROM temp_t;
# 结束会话后,临时表不存在

[var]

CREATE TEMPORARY TABLE sales(
name String COMMENT ‘产品’,
city String COMMENT ‘城市’,
sale Int32 COMMENT ‘销量’);
INSERT INTO sales VALUES
(‘椰子’,’佛山’,99),(‘雪梨’,’佛山’,77),(‘苹果’,’佛山’,88),
(‘椰子’,’广州’,80),(‘雪梨’,’广州’,80),(‘苹果’,’广州’,70);

SELECT city
,groupArray(name) OVER (PARTITION BY city)
FROM sales;

SELECT
city,
name,
sale,
rank() OVER(PARTITION BY city ORDER BY sale DESC)
FROM sales;

[var]

多数情况使用单引号

CREATE TABLE default.temp_t(
`a` String COMMENT “匹配键”
)ENGINE=Log;
# 字段注释使用双引号报错
CREATE TABLE default.temp_t(
`a` String COMMENT ‘匹配键’
)ENGINE=Log;
# 正常建表

到此这篇关于MySQL ClickHouse不同于SQL的语法介绍的文章就介绍到这了,更多相关MySQL ClickHouse内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


数据运维技术 » MySQL ClickHouse不同于SQL的语法介绍