美文网首页
sequelize-typescript 的 left join

sequelize-typescript 的 left join

作者: AsaGuo | 来源:发表于2019-02-27 16:27 被阅读2次

    1. left join (相对于base_admin)

    • sql:
    select * 
      from author
      left join book
        on book.authorId = author.id
    
    • sequelize-typescript:
        let options = {
            raw:true,
            include:[{
                model:book
            }]
        }
        
        author.findAndCountAll(options).then(results => {
            console.log("results.rows:",results.rows)
        })
    

    2. ringt join(相对于base_admin)

    备注:就是left join 的反过来查询

    • sql:
    select * 
      from book
      left join author
        on author.bookId = book.id
    
    • sequelize-typescript:
        let options = {
            raw:true,
            include:[{
                model:author,
                where:{
                    id:{
                        [Sequelize.Op.ne]:null //过滤掉无效的数据 
                    }
                }
            }]
        }
        
        book.findAndCountAll(options).then(results => {
            console.log("results.rows:",results.rows)
        })
    

    相关文章

      网友评论

          本文标题:sequelize-typescript 的 left join

          本文链接:https://www.haomeiwen.com/subject/qjpfuqtx.html