【问题标题】:How to convert query ' WHERE IN' of strings with TypeORM Query Builder?如何使用 TypeORM Query Builder 转换字符串的查询“WHERE IN”?
【发布时间】:2021-11-05 00:31:05
【问题描述】:

在以下 TypeORM 查询中出现错误:

async exportUsers(stateId: string, zipcodes: string) 
{
  //zipcodes = '60563', '54656', '94087';//
  const query = await this.userRepository
  .createQueryBuilder('user')
  .select('user.email', 'email')
  .where('left(user.Zip,5) in :zip', { zip: zipcodes }); 
}

如何使用 IN 将包含“字符串数组”的字符串传递给 TypeORM 查询。

【问题讨论】:

    标签: sql-server typeorm node.js-typeorm


    【解决方案1】:

    这行得通:

    zipcodes = '60563', '54656', '94087';
    const ziplist: string[] = zipcodes.replace("'", "").split(",");
    

    将查询更改为:

    .where('left(s.ShipToZip,5) in (:...zip)', { zip: ziplist }); 
    

    【讨论】:

      【解决方案2】:

      用以下代码替换你的 where 子句:

      .where("left(user.Zip,5) IN (:zip )", { zip : zipcodes })
      

      注意:您的邮政编码变量应该是数组格式;表示:

      zipcodes = ['94085','54656','94087']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-27
        • 2014-04-10
        • 1970-01-01
        • 2016-12-27
        • 2021-02-04
        • 1970-01-01
        • 2016-04-10
        • 1970-01-01
        相关资源
        最近更新 更多