hua123s
V2EX  ›  Node.js

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

  •  
  •   hua123s · Nov 14, 2019 · 10224 views
    This topic created in 2546 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
    zbinlin
    Nov 14, 2019
    1
    应该没问题
    wangyzj
    wangyzj
    Nov 14, 2019
    2
    可以
    sys234
    sys234
    Nov 14, 2019❤️1
    3
    ```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
    hua123s
    OP
    Nov 14, 2019 via iPhone
    4
    @sys234 emoji,是我太愚蠢了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3686 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 05:22 · PVG 13:22 · LAX 21:22 · JFK 00:22
    ♥ Do have faith in what you're doing.