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;