如何在 Sequelize 中使用 LEFT JOIN? [英] How can I use LEFT JOIN with Sequelize?
本文介绍了如何在 Sequelize 中使用 LEFT JOIN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 Sequelize 关系:
I have the following Sequelize relations:
Shop.hasMany(ShopAd, {foreignKey : 'shop_id', as : 'ads'});
ShopAd.belongsTo(Shop, {foreignKey : 'id'})
对于以下 Sequelize 查询:
For the following Sequelize query:
Shop.findAll({
where: {id:shopId},
include: [{model:ShopAd, as:'ads', where:{is_valid:1, is_vertify:1}}]
}).success(function(result) {
callback(result);
});
Sequelize 为此查询运行的 SQL 是:
the SQL that Sequelize runs for this query is:
SELECT `Shop`.`id`, `Shop`.`user_id`, `Shop`.`short_name`, `Shop`.`description`, `Shop`.`tips`, `Shop`.`city`, `Shop`.`province`, `Shop`.`address`, `Shop`.`logo`, `Shop`.`publicity_photo`, `Shop`.`taobao_link`, `Shop`.`is_vertify`, `Shop`.`create_time`, `Shop`.`update_time`, `ads`.`id` AS `ads.id`, `ads`.`shop_id` AS `ads.shop_id`, `ads`.`pic_url` AS `ads.pic_url`, `ads`.`description` AS `ads.description`, `ads`.`link` AS `ads.link`, `ads`.`is_valid` AS `ads.is_valid`, `ads`.`is_vertify` AS `ads.is_vertify`, `ads`.`create_time` AS `ads.create_time`, `ads`.`update_time` AS `ads.update_time` FROM `weshop_shop` AS `Shop`
INNER JOIN `weshop_shop_advertsing` AS `ads` ON `Shop`.`id` = `ads`.`shop_id` AND `ads`.`is_valid`=1 AND `ads`.`is_vertify`=1 WHERE `Shop`.`id`='1';
执行INNER JOIN
.
我想改用LEFT JOIN
.我如何使用 Sequelize 做到这一点?
I want to use LEFT JOIN
instead. How can I do this with Sequelize?
推荐答案
使用:必填:false
句子:
Shop.findAll({
where:{id:shopId},
include:[
{ model:ShopAd, as:'ads',
where:{
is_valid:1,
is_vertify:1},
required:false
}
]
})
.success(function(result) {
callback(result);
});
这篇关于如何在 Sequelize 中使用 LEFT JOIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文