【问题标题】:Convert SQL query to Sequelize Query Format将 SQL 查询转换为 Sequelize 查询格式
【发布时间】:2019-07-07 15:47:24
【问题描述】:

我是 Sequelize ORM 的新手。我想将 SQL 查询转换为 Sequelize 查询。

这是我的 SQL 查询,我想将此查询转换为 sequelize 查询:

SELECT * FROM `Posts` AS `Posts` 
    WHERE `Posts`.user_id IN 
        (SELECT `Follows`.receiver_id FROM `follows` AS `Follows` 
            WHERE `Follows`.user_id = user_id and `Follows`.status = "accept");

我试过了,但它没有返回任何数据:

Posts
    .findAll({ where: { 
        user_id: { [Op.in]: [{
                include: [{
                    model: Follows,
                    attributes: ['receiver_id'],
                    where: { 
                        user_id: user_id,
                        status:status
                    }
                }]
            }]
        }
    }})
    .then(users => { res.send(users); })

执行上述代码后,控制台报错

SELECT `event_id`, `user_id`, `event_message`, `e_imagepath`, 
    `createdAt`, `updatedAt`, `receiver_id` 
    FROM `Posts` AS `Posts` 
    WHERE `Posts`.`user_id` IN ('[object Object]');

我想将 SQL 查询转换为 Sequelize 查询。

【问题讨论】:

    标签: mysql express sequelize.js


    【解决方案1】:

    你把你的 incude 放在了错误的位置。据我所知,Sequelize 没有子查询功能。

    所以你可以这样做:

    Posts
        .findAll({ where: { user_id: user_id},
                   include: [{
                        model: Follows,
                        attributes: ['receiver_id'],
                        where: { 
                            user_id: user_id,
                            status:status
                        }
                    }]
    
        })
        .then(users => { res.send(users); })
    

    如果上面的示例不适合您的需要。您还可以尝试通过将原始 SQL 与 Sequelize 混合使用子查询,如下面的链接所述:

    stackoverflow.com/questions/28286811/sequelize-subquery-as-field

    【讨论】:

    • 我已经尝试过了,但这会根据 user_id 返回数据。实际上它应该根据我从 Follows 表中获得的 receiver_id 返回数据。
    • 类似这样的.....where:{user_id:{[Op.in]:[sequelize.literal('SELECT Follows.receiver_id FROM follows AS Follows WHERE @ 987654326@.user_id=1 和Follows.status="accept')]}}
    【解决方案2】:
    This works fine.
    
    router.get('/posts', function(req, res)
    {
        const user_id = req.session.user_id;
        const status = "accept";
        Posts.findAndCountAll({include:[{ model: Likes},{ model: Comments},{ model: Users}],
            where:{user_id:{[Op.in]:[sequelize.literal('SELECT `Follows`.receiver_id FROM `follows` AS `Follows` WHERE `Follows`.user_id=1 and `Follows`.status="accept')]}}
    
            })
        .then((postdata)=>
        {
    
            Users.findAll({where:{user_id:user_id}})
            .then((userdata)=>
            {
                res.send(postdata.rows)
               // res.render('home',{title:'home',items:postdata.rows,user:userdata});
            }) 
            .catch((err)=>
            {
    
            })
    
        })
        .catch((err)=>
        {
    
        })
    
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      相关资源
      最近更新 更多