mysql - FindAll WHERE 在 Sequelize 中使用包含别名字段

标签 mysql node.js express sequelize.js

如何使用别名包含关联通过 WHERE 执行 FindAll

现在我有这个代码:

 Photobook.findAndCountAll({
    include: [
      {
        attributes: ['full_name'],
        as: 'layouter_name',
        model: UserModel,
      },
      {
        attributes: ['full_name'],
        as: 'current_pic',
        model: UserModel,
      }
    ],
    where:{
       {
         '$current_pic.full_name$' : {
             $like: '%name%'
         }
      }
    }
 });

它不起作用,因为 Photobook 模型没有 current_pic.full_name 字段。我只想搜索 current_pic 关联而不是 layouter_name 关联。

如何执行类似的操作?

最佳答案

试试这个:

Photobook.findAndCountAll({
    include: [
      {
        attributes: ['full_name'],
        as: 'layouter_name',
        model: UserModel,
      },
      {
        attributes: ['full_name'],
        as: 'current_pic',
        model: UserModel,
        where: {
            full_name: {
                $like: '%name%'
            }
        },
        required: true
      }
    ]
 }); 

您可以在 include 数组本身中给出 where 条件。 required: true 确保 JOIN 被视为 INNER JOIN

关于mysql - FindAll WHERE 在 Sequelize 中使用包含别名字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46167937/

相关文章:

php - CodeIgniter 数据库迁移错误

php - 在 PHP 中将字符串递增 1

node.js - model.fetch 与相关模型 bookshelfjs

node.js - 在 Express 中生成 _csrf?

javascript - 如何从 Node.JS 套接字流触发浏览器下载?

javascript - 在不知道加密方法的情况下通过给定的密码和盐对用户进行身份验证

MySQL INSERT ...从 1 个表中选择到 2 个表中

mysql - 聚合 MySQL 中每行最近 n 天的数据

javascript - 在 Nginx 上运行 PHP 和 Node.js(PHP 的 php 扩展文件,node 的 html 扩展)

node.js - Express 和 MEAN 堆栈中的路由