首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将mysql查询转换为sequelize?

如何将mysql查询转换为sequelize?
EN

Stack Overflow用户
提问于 2018-07-20 23:53:35
回答 1查看 96关注 0票数 1

测试台

p_id   p_type   name
----------------------------------
100    Y        hong
101    Y        kim
200    N        park
201    N        John

我想在Test表中插入“102Y choi”。

mysql查询

insert into test(p_id,p_type,name) 
select (max(p_id)+1),'Y','choi' from test where p_type='Y'

sequelize中的等价查询是什么?

EN

回答 1

Stack Overflow用户

发布于 2018-07-21 04:17:27

除了在一个事务中进行两个查询之外,我看不到任何其他选择:

sequelize.transaction(t => {
  Test.findAll({
    order: [['p_id', 'DESC']],
    where: { p_type: 'Y' },
    limit: 1
  }, {transaction: t}).then(maxTest=>{
    const max = (maxTest[0] || {p_id: 0}).p_id + 1;
    return Test.create({
        p_id: max,
        p_type: 'Y',
        name: 'choi'
    }, {transaction: t})
  })
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51446280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档