0

我正在尝试按字符串列(originalUrl)的子字符串对表进行排序。

我执行了一个原始 SQL 查询,它运行良好:

SELECT * FROM photo ORDER BY SUBSTRING(originalUrl, -40, 8) ASC;

但是这段代码会打印一个错误:

const [photos, count] = await this.photoRepository
        .createQueryBuilder('photo')
        .orderBy('SUBSTRING(photo.originalUrl, -40, 8)', 'ASC')
        .getManyAndCount();

错误是:

Error: "SUBSTRING(photo" alias was not found. Maybe you forgot to join it?

我用谷歌搜索了很多次这个问题,但我找不到任何解决方案。任何帮助将不胜感激。谢谢!

编辑:

我检查了控制台,发现表中originalUrl列的别名是,所以我尝试了这段代码,效果很好!photophoto_originalUrl

const [photos, count] = await this.photoRepository
        .createQueryBuilder('photo')
        .orderBy(`SUBSTRING("photo_originalUrl", -40, 8)`, 'ASC')
        .getManyAndCount();

编辑2:

实际上,它没有用。它返回字符串的子"photo_originalUrl"字符串,而不是其列值。

4

1 回答 1

0

我尝试了一个示例查询 -

return await getRepository(Entity)
.createQueryBuilder("entity")
.orderBy('SUBSTRING(entity.sample_attribute, -40, 8)', 'ASC')
.printSql()
.getManyAndCount();

它被成功执行。我认为您的问题不是如何SUBSTRINGORDER BY. 获取存储库时可能会出现问题photo

于 2021-02-04T05:32:39.867 回答