Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

node.js - How can I use LEFT JOIN with Sequelize?

I have the following Sequelize relations:

Shop.hasMany(ShopAd, {foreignKey : 'shop_id', as : 'ads'});
ShopAd.belongsTo(Shop, {foreignKey : 'id'})

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);
});

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';

which does an INNER JOIN.

I want to use LEFT JOIN instead. How can I do this with Sequelize?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

using: required:false

sentences:

Shop.findAll({
     where:{id:shopId}, 
     include:[
         { model:ShopAd, as:'ads', 
           where:{ 
                 is_valid:1, 
                 is_vertify:1},   
           required:false
           }
         ]
      })
      .success(function(result) {
        callback(result);
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...