我有一个表,该表与其他表有一些关系(多对多和一对一)。我正在尝试使用“ where”和“ andWhere”条件的某些参数过滤数据。如果我有多个“ andWhere”,那么我总是收到空数组。
这是一个带有我的参数的对象。
{
"countryId": 375,
"cityId": 5724,
"sportTypeId": 12,
"languageId": 510,
"stringQuery": ""
}
如果我注释所有if语句,并且在这种情况下仅保留一个“ where”和一个“ andWhere”,则一切正常。在其他情况下,我会收到空数组
async findUsers(params: FindUserDto, locale: string): Promise<AuthUser[]> {
const query = this.authRepository
.createQueryBuilder('account')
.leftJoinAndSelect('account.profile', 'profile')
.leftJoinAndSelect('profile.avatarPhoto', 'avatar')
.leftJoinAndSelect('profile.coverPhoto', 'coverPhoto')
.leftJoinAndSelect('profile.primaryCountry', 'primaryCountry')
.leftJoinAndSelect('profile.primaryCity', 'primaryCity')
.leftJoinAndSelect('profile.languages', 'language')
.leftJoinAndSelect('profile.practicedSports', 'practicedSport')
.leftJoinAndSelect('practicedSport.sportType', 'sportType')
.where('account.registered = :registered', { registered: true });
if (params.cityId > 0) {
query.andWhere('primaryCity.id = :id', { id: params.cityId });
} else if (params.countryId > 0) {
query.andWhere('primaryCountry.id = :id', { id: params.countryId });
}
if (params.languageId > 0) {
query.andWhere('language.id = (:id)', { id: params.languageId });
}
if (params.sportTypeId > 0) {
query.andWhere('sportType.id = (:id)', { id: params.sportTypeId });
}
if (params.stringQuery.length > 5) {
query.andWhere(
"(profile.aboutUser -> :locale) ILIKE '%' || :sample || '%' OR (profile.interests -> :locale) ILIKE '%' || :sample || '%'",
{ locale, sample: params.stringQuery },
);
}
const users = await query.getMany();
return users;
}
更新:这是生成的sql代码
SELECT "account"."id" AS "account_id",
"account"."email" AS "account_email",
"account"."password" AS "account_password",
"account"."role" AS "account_role",
"account"."registered" AS "account_registered",
"account"."profile_id" AS "account_profile_id",
"profile"."id" AS "profile_id",
"profile"."email" AS "profile_email",
"profile"."first_name" AS "profile_first_name",
"profile"."middle_name" AS "profile_middle_name",
"profile"."last_name" AS "profile_last_name",
"profile"."about_user" AS "profile_about_user",
"profile"."interests" AS "profile_interests",
"profile"."birthday" AS "profile_birthday",
"profile"."gender" AS "profile_gender",
"profile"."phone_number" AS "profile_phone_number",
"profile"."created_at" AS "profile_created_at",
"profile"."avatar_photo_id" AS "profile_avatar_photo_id",
"profile"."cover_photo_id" AS "profile_cover_photo_id",
"profile"."default_album_id" AS "profile_default_album_id",
"profile"."primary_country_id" AS "profile_primary_country_id",
"profile"."primary_city_id" AS "profile_primary_city_id",
"profile"."primary_language_id" AS "profile_primary_language_id",
"avatar"."id" AS "avatar_id",
"avatar"."name" AS "avatar_name",
"avatar"."description" AS "avatar_description",
"avatar"."location" AS "avatar_location",
"avatar"."type" AS "avatar_type",
"avatar"."url" AS "avatar_url",
"avatar"."path" AS "avatar_path",
"avatar"."album_id" AS "avatar_album_id",
"coverPhoto"."id" AS "coverPhoto_id",
"coverPhoto"."name" AS "coverPhoto_name",
"coverPhoto"."description" AS "coverPhoto_description",
"coverPhoto"."location" AS "coverPhoto_location",
"coverPhoto"."type" AS "coverPhoto_type",
"coverPhoto"."url" AS "coverPhoto_url",
"coverPhoto"."path" AS "coverPhoto_path",
"coverPhoto"."album_id" AS "coverPhoto_album_id",
"primaryCountry"."id" AS "primaryCountry_id",
"primaryCountry"."name" AS "primaryCountry_name",
"primaryCountry"."alpha_2" AS "primaryCountry_alpha_2",
"primaryCountry"."alpha_3" AS "primaryCountry_alpha_3",
"primaryCountry"."country_code" AS "primaryCountry_country_code",
"primaryCountry"."region" AS "primaryCountry_region",
"primaryCountry"."region_code" AS "primaryCountry_region_code",
"primaryCountry"."sub_region" AS "primaryCountry_sub_region",
"primaryCountry"."sub_region_code" AS "primaryCountry_sub_region_code",
"primaryCity"."id" AS "primaryCity_id",
"primaryCity"."name" AS "primaryCity_name",
"primaryCity"."code" AS "primaryCity_code",
"primaryCity"."coordinates" AS "primaryCity_coordinates",
"primaryCity"."country_id" AS "primaryCity_country_id",
"language"."id" AS "language_id",
"language"."name" AS "language_name",
"language"."native_name" AS "language_native_name",
"language"."alpha_2" AS "language_alpha_2",
"practicedSport"."id" AS "practicedSport_id",
"practicedSport"."start_date" AS "practicedSport_start_date",
"practicedSport"."sport_id" AS "practicedSport_sport_id",
"practicedSport"."user_id" AS "practicedSport_user_id",
"sportType"."id" AS "sportType_id",
"sportType"."name" AS "sportType_name",
"sportType"."sport_category_id" AS "sportType_sport_category_id"
FROM "auth" "account"
LEFT JOIN "users" "profile" ON "profile"."id" = "account"."profile_id"
LEFT JOIN "media" "avatar" ON "avatar"."id" = "profile"."avatar_photo_id"
LEFT JOIN "media" "coverPhoto" ON "coverPhoto"."id" = "profile"."cover_photo_id"
LEFT JOIN "countries" "primaryCountry" ON "primaryCountry"."id" = "profile"."primary_country_id"
LEFT JOIN "cities" "primaryCity" ON "primaryCity"."id" = "profile"."primary_city_id"
LEFT JOIN "users_languages" "profile_language" ON "profile_language"."language_id" = "profile"."id"
LEFT JOIN "languages" "language" ON "language"."id" = "profile_language"."user_id"
LEFT JOIN "user_to_sport" "practicedSport" ON "practicedSport"."user_id" = "profile"."id"
LEFT JOIN "sport_types" "sportType" ON "sportType"."id" = "practicedSport"."sport_id"
WHERE ("profile"."about_user" -> :locale) ILIKE '%' || :sample || '%'
OR ("profile"."interests" -> :locale) ILIKE '%' || :sample || '%';
基于这种行为,我有两个问题。