Skip to content

I wonder how can i perform an sql query which joins another table for it's count() column #2353

Closed

Description

@jiananshi

Here you go:

    SELECT users.id, count(companies.area)
    FROM users
    LEFT JOIN companies
    ON users.id = companies.user_id

Activity

  1. janmeier commented on Sep 25, 2014

    @janmeier
    Member
    User.findAll({
      attributes: ['id', sequelize.fn('count', sequelize.col('companies.area'))],
      include: [Company]
    });
  2. jiananshi commented on Sep 25, 2014

    @jiananshi
    Author

    I just learned that this function can use here in this way, tks very much! @janmeier

  3. jiananshi commented on Sep 26, 2014

    @jiananshi
    Author

    @janmeier
    Additional, what if I want to set alias to the count attribute, I tried {model: sequelize.fn('count', sequelize.col('companies.area')), as: 'areas'} which does not work, tks

  4. jiananshi commented on Sep 26, 2014

    @jiananshi
    Author

    Finally I got ya searching from issuses: attributes: ['id', sequelize.fn('count', [sequelize.col('companies.area')), 'areas']],

  5. janmeier commented on Sep 26, 2014

    @janmeier
    Member

    Almost correct, I'd say wrap the whole .fn in an array instead of the col
    attributes: ['id', [sequelize.fn('count', sequelize.col('companies.area')), 'areas']],

  6. jiananshi commented on Sep 26, 2014

    @jiananshi
    Author

    @janmeier
    tks, man.
    I'm afraid I may take a look at the source code after few days, just guessing usage of apis here and there feels awful :(

  7. jiananshi commented on Sep 26, 2014

    @jiananshi
    Author

    I do, but some APIS are not fully explained, I try to copy from other APIS behaviors but usually fails.. More demos will be helpful, I think

  8. janmeier commented on Sep 26, 2014

    @janmeier
    Member

    Which API's specifically - we should definitely improve on that then?

  9. janmeier commented on Sep 26, 2014

    @janmeier
    Member

    Any APIs in particular - we could try to imrpove on that. Feel free to open issues on https://github.com/sequelize/doc for missing documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions