Skip to content

Subquery in FROM Clause #5354

Open

Description

@jpodwys

I'm testing out a hosted MySQL instance that encrypts data at rest. I need to be able to do a plain-text search on one of my columns and, in order to do so, I need to execute a subquery so I'm actually comparing unencrypted text. Below is a working query--I would really appreciate some assistance with "sequelizing" it.

SELECT
  id, ownerId, date, text
FROM (
  SELECT
    id, owner_id AS ownerId, date_format(date, '%Y-%m-%d') AS date, LOWER(text) AS text
  FROM entries
  WHERE owner_id = 2
) AS subQuery
WHERE text LIKE '%hi%'

Here's my sequelize query as it stands. I need to get the above subquery into the below sequelize query. Any ideas? Thanks for the help!

return Entry.findAndCountAll({
  attributes: [
    'id', 'ownerId', 'isPublic',
    [sequelize.fn('date_format', sequelize.col('date'), '%Y-%m-%d'), 'date'],
    [sequelize.fn('CONCAT',
      sequelize.fn('LEFT', sequelize.col('text'), 140),
      sequelize.fn('IF', 
        sequelize.literal('LENGTH(text) > 140'),
      "...", "")),
    'text']
  ],
  where: {
    ownerId: userId,
    text: {$like: '%' + text + '%'}
  },
  order: [
    ['date', 'DESC'],
    ['created_at', 'DESC']
  ],
  limit: offset,
  offset: index,
  raw: true
});

Activity

  1. mickhansen commented on Feb 3, 2016

    @mickhansen
    Contributor

    We don't support selecting from a user supplied subquery

  2. jpodwys commented on Feb 3, 2016

    @jpodwys
    Author

    Is this something you'd like sequelize to support, or is it such an edge case that I might as well just use .query?

  3. mickhansen commented on Feb 3, 2016

    @mickhansen
    Contributor

    Possibly, if we can come up with a good API for this

  4. hartherbert commented on May 5, 2019

    @hartherbert
  5. cesumilo commented on Apr 1, 2021

    @cesumilo
  6. tonypee commented on Mar 1, 2022

    @tonypee

    @mickhansen Would you consider something like:

    const ACL = sequelize.define('Test', {
      user_id: {type: DataTypes.INTEGER},
      thing_id: {type: DataTypes.INTEGER}, 
    }, { 
       subQuery: () => ` SELECT * FROM `user_thing` WHERE user_id= :userId` // obviously acl can be more complex
    }
    

    (setup associations as normal)

    When the 'subQuery' field is set, no table would be created, but the sql would be injected. And then to query:

    Thngs.find({
       include: [{ 
           model: ACL, 
           subQueryScope: { userId: 1 }
       }],
    })
    
    

    So, basically a way of treating a sub query like a normal table?

    resulting in something like

    SELECT 
       * 
    FROM 
       `things` as `ThingsModel`
    LEFT JOIN (SELECT * FROM `user_thing` WHERE user_id=1) as `acl` ON `acl`.thing_id=`ThingsModel`.id
    
  7. sanxin-lin commented on Apr 28, 2022

    @sanxin-lin

    7 years later? Any support for this?

  8. ephys commented on Apr 28, 2022

    @ephys
    Member

    The comment from Mick from 6 years ago is still relevant, we need to come up with a good API design for it before we can implement it.

    The solution @tonypee sounds more like a solution to support View Tables (#7197) than subqueries.

    The solution should probably just be a from parameter. But we need to make sure it works well with the other available options.

    return User.findAll({
      from: literal('SELECT * FROM users LIMIT 10');
    });
    

    Consider upvoting the initial post to indicate that this feature is desired, or opening a pull request that implements it.

  9. reopened this on Apr 28, 2022
  10. github-actions commented on May 13, 2022

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  11. added
    type: feature
    and removed on May 13, 2022
  12. kmind-set commented on Jun 1, 2022

    @kmind-set
  13. added theissue type on Mar 9, 2025
  14. removed
    type: feature
    on Mar 9, 2025
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

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions