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をカウントし、カウントがゼロに等しい場合にのみレコードを含めます。

したがって、次のようなものです(正確な構文を確認してください):

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 に答える