How to make a top level SELECT that isn't from a table?
I'm trying to combine two queries,
SELECT COUNT(*) FROM A
and SELECT COUNT(*) FROM B
.
SELECT (SELECT COUNT(*) FROM A), (SELECT COUNT(*) FROM B)
does what I want. How can I convert this to Kysely?
My real code also returns two scalars, so I could theoretically use a UNION ALL
- however I'm not sure if the return order is guaranteed in MySQL. I'm open to other solutions that don't have a top level SELECT.8 Replies
Hey 👋🏻
There are various ways to achieve this
Howdy 🙋♂️ Unfortunately PlanetScale/Vitess doesn't support CTEs. Thanks anyways!
Damn, well the 2nd option should do the trick, and you won't have to worry about order guarantees.
Alternative to CTEs:
ohh I see what you're doing with
sql.literal
. I'm going with your third answer because it seems cleaner (and slightly more typesafe), but your second one also works. Greatly appreciate your multiple answers - thanks!@Igal shouldn't be possible to just do a
selectFrom
without passing any parameter or just select
method directly? I'm trying to do a:
SELECT EXISTS (subquery)
queryIf we add this, it would be very confusing for newcomers, "why am I using
db.select(db.exists(db.selectFrom('person')...))
and can't chain it with from, where, etc".
You probably can do it today with:
yeah I obviusly can, but the from clause for a select query is not always required, why imposing one in the selectFrom() ? I mean, I can guess why, but do you think it makes sense to also accept no parameters there?
It doesn't make sense, you're doing
selectFrom
but from
doesn't end up in the query.
we could add a db.select((eb) => eb.exists(eb.selectFrom('person').selectAll().where('id', '=', personId))
method, can only execute or compile.