Git Product home page Git Product logo

Comments (3)

TheGrinch00 avatar TheGrinch00 commented on August 18, 2024

@MatanYadaev I believe you can already do that but you also have to join the sub-query to the main one and maybe have some GROUP BY clause.

Have you tried doing something like this?

const subquery = db
  .select({
    userId: posts.userId,
    postsCount: count().as('postsCount'),
  })
  .from(posts)
  .groupBy(posts.userId)
  .as('postsCount');

const users = await db
  .select({
    id: users.id,
    postsCount: subquery.postsCount,
  })
  .from(users)
  .innerJoin(subquery, eq(subquery.userId, posts.userId));

Does that work for you?

from drizzle-orm.

MatanYadaev avatar MatanYadaev commented on August 18, 2024

It might work, but I didn't want to use inner join in my use case. This is my expected query:

SELECT 
    users.id,
    (
        SELECT 
            COUNT(*) 
        FROM 
            posts
        WHERE 
            posts.userId = users.id
    ) AS postsCount
FROM 
    users;

from drizzle-orm.

TheGrinch00 avatar TheGrinch00 commented on August 18, 2024

Well, in that case I don't think it will be added any time soon, especially because of the presence of the alternative I suggested...

Also keep in mind that JOINs are generally faster than subqueries if you have a large dataset, so the existing solution would also be better than the feature you requested.

In this specific use-case where you put the subquery in the SELECT statement, it will be executed for every single row of users and things can get slow really fast

from drizzle-orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.