DEV Community

Sihar Simbolon
Sihar Simbolon

Posted on

4 3

Kesalahan menggunakan pattern matching not like any di PostgreSQL

Biasanya saya menggunakan pattern matching menggunakan keyword "not in".

SELECT kolom1 from tabel where kategori not in ('A', 'B')
Enter fullscreen mode Exit fullscreen mode

Kemudian saya mencoba menggunakan keyword "not like any"

SELECT kolom1 from tabel where kategori not like any (array['A', 'B'])
Enter fullscreen mode Exit fullscreen mode

Menurut saya, hasilnya akan sama. Tetapi setelah dijalankan hasilnya berbeda. Saya penasaran kenapa hasil berbeda dan setelah saya cari tahu, cara kerja keyword any itu adalah akan bernilai benar jika salah satu elemen terpenuhi. Jadi jika elemen A dibandingkan ke keyword not like any akan bernilai true karena memenuhi kriteria bukan memenuhi salah satu elemen di dalam array yaitu B. Begitu juga sebaliknya. Sementara hasil yang diinginkan adalah bukan elemen A dan juga bukan elemen B. Untuk menjawab masalah ini digunakan keyword "not like all"

SELECT kolom1 from tabel where kategori not like all (array['A', 'B'])
Enter fullscreen mode Exit fullscreen mode

Referensi
stackoverflow

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more