我如何在不同表的两列上使用sequelize.fn('ifnull')

时间:2017-06-25 07:50:49

标签: sequelize.js

这是sql:

SELECT IFNULL(A.price, B.price)
FROM table1 B
INNER JOIN (
    SELECT price
        ,product_id
    FROM table2
    WHERE user_id = xxx
    ) A
    ON A.product_id = B.product_id

如何使用sequelize.findAll()

获得相同的结果

1 个答案:

答案 0 :(得分:0)

假设你有从A到B定义的关联

Model.A.findAll({where:{user_id:XXX}, include:Model.B})

如果您没有关联,可以使用raw queries

sequelize.query('SELECT IFNULL(A.price, B.price) FROM table1 B INNER JOIN (SELECT price, product_id FROM table2 WHERE user_id=:uid) A ON A.product_id=B.product_id',{replacements:{uid:xxx}})