如何使用SQL从数组中提取数据

问题描述

如果我的字符串列包含以下数据:

+-------+----------------------------+
| Row # |        Array Column        |
+-------+----------------------------+
|     1 | {apple;coconut;mango}      |
|     2 | {coconut;pear;grapes}      |
|     3 | {strawberry;mango;coconut} |
|     4 | {}                         |
|     5 | {january}                  |
+-------+----------------------------+

我如何只取出其中有芒果的行?

结果:

+-------+----------------------------+
| Row # |        Array Column        |
+-------+----------------------------+
|     1 | {apple;coconut;mango}      |
|     3 | {strawberry;mango;coconut} |
|     4 | {}                         |
+-------+----------------------------+

解决方法

如果列是字符串而不是实际数组。

SELECT * 
FROM YourTable 
WHERE ArrayColumn='{}' 
OR ArrayColumn like '%mango%'

如果该列确实是数组

SELECT * 
FROM YourTable 
WHERE size(ArrayColumn)=0
OR array_contains(ArrayColumn,'mango')

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...