Select EXISTS

How to query boolean value if row exists or not. In pure sql it will be like this:
SELECT EXISTS(SELECT 1 FROM your_table_name WHERE id = 1 AND version = 5)
SELECT EXISTS(SELECT 1 FROM your_table_name WHERE id = 1 AND version = 5)
3 Replies
thanaen
thanaen15mo ago
I've done this, but Typescript isn't satisfied:
const rowExists = (
await db.execute(
sql`select exists (select 1 from ${myTable} where ${myTable.id} = ${someId})`,
)
).rows[0].exists;
const rowExists = (
await db.execute(
sql`select exists (select 1 from ${myTable} where ${myTable.id} = ${someId})`,
)
).rows[0].exists;
Luxaritas
Luxaritas15mo ago
You might want something like
db.select({exists: sql<boolean>`exists(…)`})…
db.select({exists: sql<boolean>`exists(…)`})…
Angelelz
Angelelz15mo ago
I believe exists return either 1 or 0, so you might want to use .mapWith(Boolean) at the end. Maybe even mapWith((value) => Boolean(+value)) I'm not sure if it's 1 or "1"
Want results from more Discord servers?
Add your server