Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 我想将两个sequelize查询合并为一个sequelize查询_Mysql_Sql_Node.js_Sequelize.js - Fatal编程技术网

Mysql 我想将两个sequelize查询合并为一个sequelize查询

Mysql 我想将两个sequelize查询合并为一个sequelize查询,mysql,sql,node.js,sequelize.js,Mysql,Sql,Node.js,Sequelize.js,我试图找到的是,首先选择那些包含我的搜索参数的评论,它将返回postId,然后选择包含所有评论的整个帖子,包括我们使用搜索参数搜索的评论 app.get('/search_', (req, res, next) => { var search = req.query.search; comment.findAll({ attributes: ['PostId'], where: { the_comments: {

我试图找到的是,首先选择那些包含我的搜索参数的评论,它将返回postId,然后选择包含所有评论的整个帖子,包括我们使用搜索参数搜索的评论

app.get('/search_', (req, res, next) => {
    var search = req.query.search;

comment.findAll({
        attributes: ['PostId'],
        where: {
            the_comments: {
                [Op.like]: `%${search}%`
            }
        }
    })
    .then(data => {
        var array = [ ];
        for (var x in data) {
            console.log(data[x].PostId);
            array.push(data[x].PostId);
        }
        console.log(array);
        Post.findAll({
                include: [
                    {
                        model: comment
                    }
                ],
                where: {
                    id: {
                        [Op. in]: [array]
                    }
                }
            })
            .then(result => {
                res.json(result);
         })
    })

});
你可以试试这个

Post.findAll({
    include: [
        {
            model: comment,
            required: true,
            where: {
                    the_comments: {
                                    [Op.like]: `%${search}%`
                                  }
                   }
        }
    ]
})

谢谢你的回复,兄弟,但你没有告诉我我要找的是:将这个原始sql查询转换为等效的单一Sequelize查询这里是原始查询。选择*FROM Post p internal JOIN comment c ON p.id=c.PostId where p.id in SELECT PostId FROM comment where_comment where像%${search}%No,这将只返回那些像%${search}%这样的注释,但是如果有任何像%${search}%这样的注释,我们想要得到所有的注释,这就是上面绿色勾选答案成立的原因,顺便说一下,谢谢你的回复。
Post.findAll({
    include: [
        {
            model: comment,
            required: true,
            where: {
                    the_comments: {
                                    [Op.like]: `%${search}%`
                                  }
                   }
        }
    ]
})