November 1, 2010

Как получить пустое множество в IN

Прочитал в рассылке интересную дискуссию. Вопрос довольно злободневный, но, честно говоря, раньше не знал об этом.

Допустим надо проверить, что поле входит в какое-то множество. Очевидно, что надо использовать конструкцию типа

field IN (1, 2, 3, ...)

Но что если это множество пустое. Как его указать?

field IN (???)

Действительно, просто пустые скобки указать нельзя, получим ошибку.

postgres@localhost:5432 test=#
SELECT 1 IN ();
ERROR: syntax error at or near ")"
LINE 1: SELECT 1 IN ();
^


Просто NULL тоже нельзя.

postgres@localhost:5432 test=#
SELECT 1 IN NULL;
ERROR: syntax error at or near "NULL"
LINE 1: SELECT 1 IN NULL;
^


NULL в скобках опять нельзя, т.к. в результате всегда будет NULL.

postgres@localhost:5432 test=#
SELECT 1 IN (NULL);
?column?
----------

(1 row)


Как тогда быть? Ответ получается из вопроса - просто получить пустое множество.

postgres@localhost:5432 test=#
SELECT 1 IN (SELECT 1 WHERE FALSE);
?column?
----------
f
(1 row)

postgres@localhost:5432 test=#
SELECT 1 NOT IN (SELECT 1 WHERE FALSE);
?column?
----------
t
(1 row)

2 comments:

Anonymous said...

in - это: =any

поэйтому просто select 1 =any ( array[]::integer[] )

goLiveid Blog said...

Home Interior & Renovation Services in Chandigarh
Farming is the only profession where you can work 16-hour days, get sunburned in the middle of winter, and still be told you’re “living the simple life.” It’s a magical world where weather forecasts are more suspenseful than a thriller movie—will it rain, or will you just stand outside shaking your fist at the sky in frustration? Either way, Mother Nature is in charge, and she has a sense of humor.

Let’s talk about farm animals—those adorable, fluffy creatures that turn into rebellious hooligans the moment you turn your back. Chickens? Escape artists with no respect for fences. Cows? The real CEOs of the farm, casually blocking roads like they own them. And don’t even get me started on goats—those four-legged acrobats will climb on anything, including your patience.

Farming is also where advanced technology meets good old-fashioned stubbornness. Farmers can program GPS-guided tractors, monitor soil health with satellite imagery, and still fix a broken fence with nothing but baling wire and sheer determination. If farmers ever went to space, NASA would be amazed at how they could fix a rocket with duct tape and a hammer.
Gurugram Home Refurbishment Services
Then there’s the joy of growing crops, a process that requires backbreaking labor, endless weeding, and an optimistic spirit. You plant seeds with hope, nurture them with love, and then watch in horror as a single rabbit undoes three months of work in one afternoon. If farming teaches anything, it’s patience—and the importance of having a dog that actually chases pests instead of sleeping on the job.

At the end of the day, despite the unpredictable weather, mischievous animals, and never-ending work, farming is a life filled with humor, adventure, and a deep appreciation for the land. Sure, you might spend more time talking to your tractor than to actual people, but nothing beats the feeling of harvesting a field you nurtured from the ground up—except maybe a nap in the hay after all that hard work. 🌾🚜😆

Post a Comment