Skip to content

Latest commit

 

History

History
26 lines (18 loc) · 826 Bytes

unnest-csv.md

File metadata and controls

26 lines (18 loc) · 826 Bytes

Using unnest() to use a comma-separated string as the input to an IN query

django-sql-dashboard lets you define a SQL query plus one or more text inputs that the user can provide in order to execute the query.

I wanted the user to provide a comma-separated list of IDs which I would then use as input to a WHERE column IN ... query.

I figured out how to do that using the unnest() function and regexp_split_to_array:

select * from report where id in (select unnest(regexp_split_to_array(%(ids)s, ',')))

The ids string passed to this query is split on commas and used for the IN clause.

Here's a simple demo of how unnest() works:

select unnest(regexp_split_to_array('1,2,3', ','))

Output:

unnest
1
2
3