[✅ 원하는 내용 셀렉트 하기]
*innerJoin 한 테이블의 속성들도 select를 통해서 원하는 정보만 반환할 수 있음.
(Join한 Table의 결과를 모두 반환하고 싶다면 InnerJoinAndSelect이나 leftJoinAndSelect를 사용하면 됨.)
const user = await this.usersRepository
.createQueryBuilder('user')
.innerJoin('user.likes', 'likes')
.innerJoin('likes.product', 'product')
.select([
'user.email',
'user.name',
'user.phone_number',
'user.address',
'user.point',
'likes.id',
'likes.created_at',
'product.product_name',
'product.product_price',
'product.product_thumbnail',
])
.where('user.id = :id', { id: user_id })
.getOne();
[✅ 개수 카운트해서 객체로 반환하기]
-loadRelationCountAndMap
const user = await this.usersEntity
.loadRelationCountAndMap('user.like_count', 'user.likes') // 이 부분
.getOne();
[단순히 해당 엔티티 속성을 셀렉하지 않고, 연결한 엔티티의 속성만 셀렉하면 된다면 아래처럼 해도 된다.]
=> 하지만 이것도 @JoinColumn 설정하지 않으면 불안정함.
const reservationData = await this.reservationRepository.find({
where: { User_id: user_id },
relations: ['performance'],
select: {
id: true,
name: true
},
});
const reservationData = await this.reservationRepository.find({
where: { User_id: user_id },
relations: ['performance'],
select: {
performance: { perf_name: true, perf_price: true },
},
});
'Database > mysql' 카테고리의 다른 글
| [mysql] 프로그래머스 SQL 문제 풀기 (0) | 2023.12.16 |
|---|---|
| [database] 시퀀스, MYSQL (1) | 2023.11.26 |
| [mysql] DB 연결 오류: Error: connect ECONNREFUSED ::1:3306 (1) | 2023.10.23 |
| [TypeORM] QueryBuilder vs Repository (0) | 2023.09.05 |
| [seqelize] 테이블 생성 migration (0) | 2023.07.05 |