【clickhouse】Array数组查询操作

本文介绍了如何在Cassandra数据库的MergeTree引擎中使用arrayExists和coalesce函数进行数组搜索,包括模糊搜索tag1列中的元素和搜索tag2列中的数值,展示了在实际查询中的应用场景。
摘要由CSDN通过智能技术生成

1 测试数据

// 建库
create database test;

// 建表
CREATE TABLE test.test (
`id` String,
`tag1` Array(String),
`tag2` Array(Int32),
`updated` DateTime
)
ENGINE = MergeTree
ORDER BY id

// 插入数据
insert into test.test values ('1',['a'],[2,3],'2020-06-01 12:00:00');
insert into test.test values ('10',['a','b'],[1],'2024-01-19 22:00:00'),('11',['v','dd','qqq'],[2,3,6,4],'2020-06-02 12:00:00');

在这里插入图片描述

在这里插入图片描述

2 查询

Array(String)

// 模糊搜索tag1列存在 d 的数据
SELECT * FROM `test`.`test` where arrayExists(x -> coalesce(x, '') like '%dd%', tag1) > 0;

// 搜索tag1列存在 dd 的数据
SELECT * FROM `test`.`test` where arrayExists(x -> coalesce(x, '') = 'dd', tag1) > 0;

在这里插入图片描述
在这里插入图片描述

Array(Int32)

// 搜索tag2列存在 2 的数据
SELECT * FROM `test`.`test` where arrayExists(x -> coalesce(x, 0) = 2, tag2) > 0;

3 所用函数

3.1 arrayExists

arrayExists([func,] arr1, ...)

判断数组里是否含有元素,第一个参数是表达式,表示x经过运算是否=元素,第二个参数即是数组,可用ck列名代替,如果含有即是1,不含即为0。

搭配>0或=1使用。

3.2 coalesce

coalesce可将NULL值替换成空字符串或0,便于操作
语法:

SELECT COALESCE(column_name, '') FROM table_name;

SELECT COALESCE(column_name, 0) FROM table_name;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微雨停了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值