在 sequelize 中查询 where 关联?

Posted

技术标签:

【中文标题】在 sequelize 中查询 where 关联?【英文标题】:querying on where association in sequelize? 【发布时间】:2016-12-19 12:28:12 【问题描述】:

where 子句用于内部连接的 sequelize。 我的查询是

SELECT Cou.country_id,cou.country_name, Sta.state_id, Sta.state_name
FROM hp_country Cou
INNER JOIN hp_state Sta ON Cou.country_id = Sta.hp_country_id
WHERE (Cou.country_status=1 AND Sta.state_status=1 AND Cou.country_id=1)
AND (Sta.state_name LIKE '%ta%');

我在sequelize中写的代码是

hp_country.findAll(
    where: 
        '$hp_state.state_status$': 1
    ,
    include: [
        model: hp_state
    ]
)

它产生的错误是:

SELECT `hp_country`.`country_id`, `hp_country`.`country_name`, `hp_country`.`country_status`, `hp_country`.`created_date`, `hp_country`.`update_date` FROM `hp_country` AS `hp_country` WHERE `hp_state`.`state_status` = 1;
Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'hp_state.state_status' in 'where clause'

【问题讨论】:

【参考方案1】:
hp_country.findAll(
where: 
    //main AND condition
    $and: [
        //first joint condition
        
            $and: [
                 country_status: 1 ,
                 country_id: country_id ,
                Sequelize.literal("hp_states.state_status = 1"),
                Sequelize.literal("`hp_states.hp_districts`.`district_status`=1"),
                Sequelize.literal("`hp_states.hp_districts.hp_cities`.`city_status`=1"),
                Sequelize.literal("`hp_states.hp_districts.hp_cities.hp_locations`.`location_status`=1"),
                Sequelize.literal("`hp_states.hp_districts.hp_cities.hp_locations.hp_sub_locations`.`sub_location_status`=1"),
                Sequelize.literal("`hp_states.hp_districts.hp_cities`.`city_name` LIKE '%"+city+"%'")


            ]
        ,

        
            $or: [
                Sequelize.literal("`hp_states.hp_districts.hp_cities.hp_locations`.`location_name` LIKE '%"+query+"%'"),
                Sequelize.literal("`hp_states.hp_districts.hp_cities.hp_locations.hp_sub_locations`.`sub_location_name` LIKE '%"+query+"%'"),
                Sequelize.literal("`hp_states.hp_districts.hp_cities.hp_locations.hp_sub_locations.hp_property`.`property_name` LIKE '%"+query+"%'"),
                Sequelize.literal("`hp_states.hp_districts.hp_cities.hp_locations.hp_sub_locations.hp_property.hp_builder`.`builders_name` LIKE '%"+query+"%'")

            ]
        
    ]
,
attributes: ['country_id', 'country_name'],
required:true,
include: [
    
        model: hp_state,
        attributes: ['state_id', 'state_name'],
        required:true,

        include: [
            
                model: hp_district,
                attributes: ['district_id', 'district_name'],
                required:true,
                include: [
                    
                        model: hp_city,
                        attributes: ['city_id', 'city_name'],
                        required:true,

                        include: [
                            
                                model: hp_location,
                                attributes: ['location_id', 'location_name'],
                                required:true,
                                include: [
                                    
                                        model: hp_sub_location,
                                        attributes: ['sub_location_id', 'sub_location_name'],
                                        required:true,
                                        include: [
                                            
                                                model: hp_property,
                                                attributes: ['property_id', 'property_name'],
                                                required: true,
                                                include: [
                                                    
                                                        model:hp_builders,
                                                        attributes: ['builders_id', 'builders_name'],
                                                        required: true

                                                    
                                                    ]
                                            
                                            ]


                                    ]

                            ]

                    ]
            
        ]
    
]

)

【讨论】:

这仅在关系为 1:N 时才有效。如果联接应该是外部联接怎么办?【参考方案2】:

您的 Sequelize 代码应如下所示:

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,
            state_name: 
                $like: '%ta%'
            
        
    ]
);

要仅选择某些属性,您可以使用attributes 选项。 where 子句应该移到 include 语句中,因为您使用的条件与 hp_state 模型有关。

【讨论】:

,感谢您的回复,我知道您发布的内容,但是我想在状态包含框中获取 country_id 的列名,因为我想检查此条件,请参见此处 [where (Cou. country_status=1 AND St​​a.state_status=1 AND Cou.country_id=1) AND (Sta.state_name LIKE '%ta%');] @SimhaChalam 看来我没有正确理解你,但我已经更新了上面的代码以满足你的所有条件。 @SimhaChalam 如果您需要在include 中使用hp_country 表的任何列,您可以使用$col 运算符或Sequelize.col 函数。 是的,我尝试过这种方式。但它的说法 col 不是他们的 sequalize 因为我想包含这么多内部连接并且从父模型到子模型列名有一个沉重的关联

以上是关于在 sequelize 中查询 where 关联?的主要内容,如果未能解决你的问题,请参考以下文章

Sequelize - 在 where 子句中查询其他表

在子查询中续集“WHERE”子句

Sequelize 关联和查询此 mysql 查询

查询联结表而不在 Sequelize 中获取两个关联

Sequelize - 按关联计数排序

如何在 Sequelize 中对 belongsToMany 关联进行***查询?