Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@vercel/postgres - can't do 'SELECT x WHERE id IN' query #514

Closed
gillyb opened this issue Dec 7, 2023 · 2 comments
Closed

@vercel/postgres - can't do 'SELECT x WHERE id IN' query #514

gillyb opened this issue Dec 7, 2023 · 2 comments

Comments

@gillyb
Copy link
Contributor

gillyb commented Dec 7, 2023

I need to do a SELECT IN query - to select multiple rows, but can't seem to do it. I tried many ways -

const ids = ['aaa', 'bbb', 'ccc'];
const {rows} = sql`select * from my_table where id in (${ids})`;

I also tried concatenating the ids on my own, but it also doesn't seem to work.

How would I execute a query like this ?

@elliott-with-the-longest-name-on-github
Copy link
Collaborator

Right now, you'll have to fall back to creating the query and calling sql.query yourself -- SQL templating is very limited:

const query = 'SELECT * FROM users WHERE id IN ($1, $2, $3)'
const params = [1234, 2345, 3456]
await sql.query(query, params)

However, when/if #504 merges, something like this will be possible:

await sql`SELECT * FROM users WHERE id IN (${LIST(params)})`

So hopefully you don't have to manually build more-complex queries for too long!

@gillyb
Copy link
Contributor Author

gillyb commented Dec 8, 2023

That TQL syntax looks great, would happy to see it merged.
For now, thanks a lot for the response, I appreciate it :)

@gillyb gillyb closed this as completed Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants