Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql - "IN empty array" syntax

Tags:

postgresql

I create programmatically sql queries to Postgresql like this:

SELECT * FROM table WHERE column IN (1, 2, 3);
SELECT * FROM table WHERE column IN (1);
SELECT * FROM table WHERE column IN ();

The last query generates a syntax error. How should I handle this special case and write an empty array here?

like image 221
sanyassh Avatar asked Oct 18 '25 15:10

sanyassh


1 Answers

Internally,

col IN (1, 2, 3)

is converted to

col = ANY (ARRAY[1,2,3]::integer[])

That syntax will also work for empty arrays:

col = ANY (ARRAY[]::integer[])
like image 74
Laurenz Albe Avatar answered Oct 21 '25 11:10

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!