Expect orWhere()与andWhere()代替where()

问题描述 投票:0回答:1

我有一个查询:

topics = await topicRepository.createQueryBuilder('topic')
                               .leftJoinAndSelect('topic.user', 'user', 'topic.userId = user.id')
                               .where('topic.categoryId = :id', {
                                       id: categoryId,
                                     })
                               .andWhere('topic.title like :search', { search: `%${searchKey}%`})
                               // It should take the first where
                               .orWhere('user.pseudo like :search', { search: `%${searchKey}%` })
                               .addOrderBy(filter === 'latest' ? 'topic.created_at' : 'topic.repliesCount', 'DESC')
                               .take(limit)
                               .skip(skip)
                               .getMany();

生成的SQL查询是:

SELECT DISTINCT distinctAliastopic_id as \“ ids_topic_id \”,distinctAliastopic_created_at FROM(SELECT topicid AS topic_idtopictitle AS [C0 ],topic_titletopic AS contenttopic_contenttopic AS created_attopic_created_attopic AS viewstopic_viewstopic AS repliesCounttopic_repliesCounttopic AS categoryIdtopic_categoryIdtopic AS userIdtopic_userIdtopic AS surveyIdtopic_surveyIduser AS id,[C0 ]。user_id AS useremailuser_email AS userpseudouser_pseudo AS userpassworduser_password AS userrankuser_rankAS useravataruser_avatar AS usercreatedAtuser_createdAt AS userlastActivityuser_lastActivity AS usersignature。[C0 ] AS user_signatureuserpost_count ASuser_post_countfromuserupdatedAt左联接user_updatedAttopicON topicuser = useruser和( topic.userId = idtopic)WHERE topic.categoryId ='5'和userIduser如'%admin%'或topic.user.pseudo如'%admin%')id ORDER BY topictitle DESC,distinctAlias ASC LIMIT 20

问题在这里:

[WHERE topic.categoryId ='5'AND topic.title如'%admin%'或topic.user.pseudo如'%admin%')

我期望:

[WHERE(topic.categoryId ='5'AND topic.title如'%admin%')或(topic.categoryId ='5'AND topic.user.pseudo如'%admin%')

我希望。orWhere。andWhere的OR,而不是。where

我找不到有关此用例的任何文档/问题。

query-builder typeorm
1个回答
0
投票

查询条件的优先级可以通过使用distinctAlias类来控制:

topic_created_at
© www.soinside.com 2019 - 2024. All rights reserved.