0

我正在使用 postgres 来提取一些数据。我有一个数组(类别),我想排除包含“>”的结果

select title, short_url, unnest(categories) as cats, winning_offer_amount
from auctions
where ended_at is not null
and '% > %' NOT IN cats
group by title, short_url, cats, winning_offer_amount

我意识到我的语法完全错误,但试图给出我想要写的东西的想法。结果可能是:

Women's > Shoes
Women's
Men's > Shoes
Men's

我想用'>'排除结果

4

2 回答 2

3

一个简单的“蛮力”方法是将数组转换为text并检查:

SELECT title, short_url, categories, winning_offer_amount
FROM   auctions
WHERE  ended_at IS NOT NULL
AND    categories::text NOT LIKE '% > %';  -- including blanks?

一个干净优雅unnest()NOT EXISTS半连接解决方​​案:

SELECT title, short_url, categories, winning_offer_amount
FROM   auctions a
WHERE  ended_at IS NOT NULL
AND    NOT EXISTS (
   SELECT 1
   FROM   unnest(a.categories) AS cat
   WHERE  cat LIKE '% > %'
   );

SQL小提琴。

于 2014-09-23T20:08:24.190 回答
0

'>'计算字符出现的次数,cats并且仅在计数等于 0 时才包括记录。

所以,像这样(检查确切的语法):

select title, short_url, unnest(categories) as cats, winning_offer_amount
from auctions
where ended_at is not null
and (length(cats) - length(replace(cats, '>', '')))=0 
group by title, short_url, cats, winning_offer_amount
于 2014-09-23T18:20:31.663 回答