V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
hua123s
V2EX  ›  Node.js

nodejs sequelize 的问题 问个问题?这种可以用 include 来实现?

  •  
  •   hua123s · Nov 14, 2019 · 9734 views
    This topic created in 2359 days ago, the information mentioned may be changed or developed.
    SELECT 
    users.id,SUM(b)
    FROM 
    users LEFT JOIN c 
    on c.d like CONCAT(users.d,'%')
    GROUP BY users.id
    
    4 replies    2019-11-14 20:53:21 +08:00
    zbinlin
        1
    zbinlin  
       Nov 14, 2019
    应该没问题
    wangyzj
        2
    wangyzj  
       Nov 14, 2019
    可以
    sys234
        3
    sys234  
       Nov 14, 2019   ❤️ 1
    ```javascript
    //tableName=>Model
    //users=>Users; c=>ModelC

    Users.findAll({
    attributes: ["id", [fn("SUM", col("b")), "aliasB"]],
    include: [{
    association: Users.belongsTo(ModelC, {
    foreignKey: "d"
    }),
    model: ModelC,
    attributes: [],
    required: false,
    on: {
    d: {
    [Op.like]: fn("concat", literal("users.d"), "%")
    }
    }
    }],
    group: [literal("users.id")],
    limit: 10
    }).then(result => {
    console.log(JSON.stringify(result));
    }).catch(err => { });
    ```
    hua123s
        4
    hua123s  
    OP
       Nov 14, 2019 via iPhone
    @sys234 emoji,是我太愚蠢了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   823 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 20:18 · PVG 04:18 · LAX 13:18 · JFK 16:18
    ♥ Do have faith in what you're doing.