我有类似的问题this one(最相似的是&&
的答案).对于postgres,我想得到数组列和python列表的交集.我试着用&&
操作符做到这一点:
query(Table.array_column.op('&&')(cast(['a', 'b'], ARRAY(Unicode)))).filter(Table.array_column.op('&&')(cast(['a', 'b'], ARRAY(Unicode))))
但是op('&&')
似乎返回bool
类型(对filter有意义),而不是交集。
因此,对于表数据:
id | array_column
1 {'7', 'xyz', 'a'}
2 {'b', 'c', 'd'}
3 {'x', 'y', 'ab'}
4 {'ab', 'ba', ''}
5 {'a', 'b', 'ab'}
我想得到:
id | array_column
1 {'a'}
2 {'b'}
5 {'a', 'b'}
1条答案
按热度按时间bakd9h0s1#
一种方法 * 是取消嵌套数组列,然后重新聚合与列表值匹配的行,按id分组。这可以作为子查询来完成:
等效的SQLAlchemy构造为:
返回