【问题标题】:Combined WHERE clause for all INCLUDEs所有 INCLUDE 的组合 WHERE 子句
【发布时间】:2016-08-16 05:18:19
【问题描述】:
i want to perform this query in sequelize models:

SELECT Cou.country_id,cou.country_name, Sta.state_id, Sta.state_name, Dis.district_id,
Dis.district_name,Cit.city_id, Cit.city_name,Loc.location_id,Loc.location_name,Sub_Loc.sub_location_id,Sub_Loc.sub_location_name,Prop.property_id,Prop.property_name,Prop.hp_builders_id,
Bud.builders_id,Bud.builders_name
FROM hp_country Cou
INNER JOIN hp_state Sta ON Cou.country_id = Sta.hp_country_id
INNER JOIN hp_district Dis ON Sta.state_id = Dis.hp_state_id
INNER JOIN hp_city Cit ON Dis.district_id = Cit.hp_district_id
INNER JOIN hp_location Loc ON Cit.city_id = Loc.hp_city_id
INNER JOIN hp_sub_location Sub_Loc ON Loc.location_id = Sub_Loc.hp_location_id
INNER JOIN hp_property Prop ON Sub_Loc.sub_location_id=Prop.hp_sub_location_id
LEFT JOIN hp_builders Bud ON Prop.hp_builders_id=Bud.builders_id
where (Cou.country_status=1 AND Sta.state_status=1 AND Cou.country_id=1)
AND (Dis.district_name LIKE '%ky%' OR Cit.city_name LIKE '%ky%' OR Loc.location_name LIKE '%ky%' OR Sub_Loc.sub_location_name LIKE '%ky%' OR Prop.property_name LIKE '%ky%')

我尝试以这种方式编写,但我没有达到我的目标查询: 包含更多嵌套模型,因为所有模型都是内部连接,我尝试在 include box 中使用 required 是 true。如何为不同表中的列执行 [OR] 条件

hp_country.findAll({
    attributes: ['country_id', 'country_name'],
    where: {
        country_status: 1,
        country_id: 1
    },
    include: [{
        model: hp_state,
        attributes: ['state_id', 'state_name'],
        where: {
            state_status: 1,
          },
        include:[{
            model: hp_districts,
            attributes: ['district_id', 'district_name'],
            where: {
               district_status: 1
            },
            include:[{
                model: hp_city,
                attributes: ['city_id', 'city_name'],
                where: {
                    city_status: 1,
                    city_name :{
                        $like: '%ma%'
                    }
                },
                include:[{
                    model: hp_location,
                    attributes: ['location_id', 'location_name'],
                    where: {
                       location_status: 1,
                        location_name :{
                            $like: '%ma%'
                        }
                    },
                    include:[{
                        model: hp_sub_location,
                        attributes: ['sub_location_id', 'sub_location_name'],
                        where: {
                            sub_location_status: 1,
                            sub_location_name :{
                                $like: '%ma%'
                            }
                        }
                    }]
                }]

            }]
        }]
    }]
})

【问题讨论】:

  • 您是否定义了表之间的关联?
  • 是的,表已经存在并设置了完美的关系,但我对查询中的这一行感到困惑 [AND (Dis.district_name LIKE '%ky%' OR Cit.city_name LIKE '%ky% ' OR Loc.location_name LIKE '%ky%' OR Sub_Loc.sub_location_name LIKE '%ky%' OR Prop.property_name LIKE '%ky%')],如何执行或与不同表中不同列的关系
  • 其实seuqelize不允许引用模型实例,你必须使用Sequelize.colSequelize.literal来做。如果你已经完美地定义了所有关联,你就不必嵌套。
  • 请你举一些例子来在 Sequelize.col 或 Sequelize.literal 中执行这个查询
  • 当然,我会尝试解决您的问题

标签: javascript mysql node.js sequelize.js


【解决方案1】:

我尝试修复您的查询,但没有对其进行测试。如果您遇到任何问题,请通知我:

hp_country.findAll({
        attributes: ['country_id', 'country_name'],
        where: {
            //main AND condition
            $and: [
                //first joint condition
                {
                    $and: [
                        { country_status: 1 },
                        { country_id: 1 },
                        Sequelize.literal("Sta.state_status = 1")   //I put 'state_status' here because it was a joint condition to be true
                    ],
                },
                //second joint condition
                {
                    $or: [
                        Sequelize.literal("Dis.district_name LIKE '%ky%'"),
                        Sequelize.literal("Cit.city_name LIKE '%ky%'"),
                        Sequelize.literal("Loc.location_name LIKE '%ky%' "),
                        Sequelize.literal("Sub_Loc.sub_location_name LIKE '%ky%'"),
                        Sequelize.literal("Prop.property_name LIKE '%ky%'")
                    ]
                }
            ]
        },
        include: [
            {
                model: hp_state,
                attributes: ['state_id', 'state_name']
            },
            {
                model: hp_districts,
                attributes: ['district_id', 'district_name']
            },
            {
                model: hp_city,
                attributes: ['city_id', 'city_name']
            },
            {
                model: hp_location,
                attributes: ['location_id', 'location_name']
            },
            {
                model: hp_sub_location,
                attributes: ['sub_location_id', 'sub_location_name']
            }
        ]
    })

【讨论】:

  • thanq ,但我收到此错误 TypeError: sequelize.literal is not a function。我也有一个 sequalize 对象
  • hp_country.findAll({ where: Sequelize.literal(), include: [ { model: hp_state, } ] }) 这是有效的
  • 我想你有一个大写 S 和小写 s 的参考
  • 看到这个也有效,当 Sequelize 在 where 条件下不起作用时 .. hp_country.findAll({ where: Sequelize.literal("hp_states.state_name LIKE '%ka%'"),包括:[{模型:hp_state,}]})。我得到了这个的输出
  • sequelize 生成什么查询?启用其日志记录并在控制台中查看查询。
猜你喜欢
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多