我有一个查询:
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
distinctAlias
。topic_id
as \“ ids_topic_id \”,distinctAlias
。topic_created_at
FROM(SELECTtopic
。id
AStopic_id
,topic
。title
AS [C0 ],topic_title
。topic
AScontent
,topic_content
。topic
AScreated_at
,topic_created_at
。topic
ASviews
,topic_views
。topic
ASrepliesCount
,topic_repliesCount
。topic
AScategoryId
,topic_categoryId
。topic
ASuserId
,topic_userId
。topic
ASsurveyId
,topic_surveyId
。user
ASid
,[C0 ]。user_id
ASuser
,user_email
ASuser
,pseudo
。user_pseudo
ASuser
,password
。user_password
ASuser
,rank
。user_rank
ASuser
,avatar
。user_avatar
ASuser
,createdAt
。user_createdAt
ASuser
,lastActivity
。user_lastActivity
ASuser
,signature
。[C0 ] ASuser_signature
,user
。post_count
ASuser_post_count
fromuser
updatedAt
左联接user_updatedAt
topic
ONtopic
。user
=user
。user
和( topic.userId =id
。topic
)WHERE topic.categoryId ='5'和userId
。user
如'%admin%'或topic.user.pseudo如'%admin%')id
ORDER BYtopic
。title
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
我找不到有关此用例的任何文档/问题。
查询条件的优先级可以通过使用distinctAlias
类来控制:
topic_created_at