728x90
반응형
[Springboot] 21. 계좌 상세 보기 기능 (1단계)
1. 출금, 입금, 이체(입금/출금) 쿼리
(1) 입출금 내역 확인하기
use mybank;
-- 1번: 계좌에서 출금 계좌
select* from history_tb;
select*from history_tb where w_account_id='1'; -- 출금 내역
select*from history_tb where d_account_id='1'; -- 입금 내역
(2) 입출금 내역과 계좌번호 확인하기
use mybank;
-- 1단계
-- 1번 계좌의 입출금 내역
select* from history_tb;
select*from history_tb where w_account_id='1'; -- 출금 내역
select*from history_tb where d_account_id='1'; -- 입금 내역
-- 2단계
-- 계좌 번호와 함께 출력하기
-- 모든 계좌에 대한 출금 내역을 확인하고 싶다.
-- 1번 계좌에 대한 출금 내역을 보여줘
select h.id,h.amount,h.w_balance, a.number,h.created_at
from history_tb as h
join account_tb as a
on h.w_account_id=a.id
where h.w_account_id='1';
-- 3단계: 입금 내역과 계좌 번호가 함께 뜨게끔 출력해보자.
select h.id, h.amount,h.d_balance,a.number,h.created_at
from history_tb as h
join account_tb as a
on h.d_account_id=a.id;
💡쿼리 성능 증대를 위한 방법-> 인덱스
*컬럼에 인덱스를 설정하면 where 조건 연산 / JOIN 연산의 성능을 향상시킬 수 있다.
*select 절에 필요한 컬럼만 선택하면 데이터의 양을 줄일 수 있다.
(3) COALESCE 함수와 CAST 함수 써보기
-COALESCE 함수: 인자로 주어진 값들 중에서, 첫번째 NULL이 아닌 값을 반환한다.
만약 모든 인자가 NULL이라면 NULL을 반환한다.
-CAST 함수: MySQL에서는 signed와 함께 사용된다. 다운캐스팅(강제 형변환) 시에 사용된다.
-- 4단계: COALESCE 함수와 CAST 함수 사용해보기
SELECT COALESCE(NULL, null, 'third_value', 'fourth_value');
select cast('123' as INT);
select cast('123' as signed);
select cast('123' as unsigned);
(4) COALESCE 함수를 사용하여 이체/무통장입금을 명시하기
-- 5단계:
-- 출금이 만약 이체라면, 키값에 receiver 금액의 대상을 출력하기
select h.id,h.amount,h.w_balance, a.number,h.created_at,
coalesce(cast(h.d_account_id as char(10)), 'ATM') as reciever
from history_tb as h
join account_tb as a
on h.w_account_id=a.id
where h.w_account_id='1';
-- 입금 내역 출력하기
select h.id,h.amount,h.w_balance,a.number,h.created_at,
coalesce(cast(h.d_account_id as char(10)), 'ATM') as sender
from history_tb as h
join account_tb as a
on h.d_account_id=a.id
where h.d_account_id=1;
desc history_tb;
(5) 입, 출금에 대한 모든 정보 출력하기
-- 4단계
-- 입금, 출력, 이체 전체 쿼리 확인 --> 해당하는 계좌에 대해서
select h.id, h.amount,
case
when h.w_account_id=1 then (h.w_balance)
when h.d_account_id=1 then (h.d_balance)
end as balance,
coalesce(cast(wa.number as char(10)),'ATM') as sender,
coalesce(cast(da.number as char(10)),'ATM') as receiver,
h.created_at
from history_tb as h
left join account_tb as wa on h.w_account_id=wa.id
left join account_tb as da on h.d_account_id=da.id
where h.w_account_id='1' or h.d_account_id='1';
(6) 프로그램 상에서 사용할 쿼리문 정리
-- 1.
-- 출금 내역 쿼리
-- 1번계좌에 대한 출금 내역만 확인
select h.id, h.amount, h.w_balance as balance, h.created_at,
coalesce(cast(da.number as CHAR(10)), 'ATM') as receiver,
wa.number as sender
from history_tb as h
left join account_tb as wa on wa.id=h.w_account_id
left join account_tb as da on da.id = h.d_account_id
where h.w_account_id='1';
-- 입금 내역 쿼리
-- 1번계좌에 대한 출금 내역만 확인
select h.id, h.amount, h.d_balance as balance, h.created_at,
coalesce(CAST(wa.number as CHAR(10)) , 'ATM') as sender,
da.number as receiver
from history_tb as h
left join account_tb as wa on wa.id = h.w_account_id
left join account_tb as da on da.id = h.d_account_id
where h.d_account_id = 1;
-- 입출금 쿼리
-- 1번 계좌에 대한 입금,출금 내역 전체
select h.id, h.amount,
case
when h.w_account_id = 1 then (h.w_balance)
when h.d_account_id = 1 then (h.d_balance)
end as balance,
coalesce(cast(wa.number as char(10)), 'ATM') as sender,
coalesce(cast(da.number as char(10)), 'ATM') as receiver,
h.created_at
from history_tb as h
left join account_tb as wa on h.w_account_id = wa.id
left join account_tb as da on h.d_account_id = da.id
where h.w_account_id = 1 OR h.d_account_id = 1;
728x90
반응형
'Springboot' 카테고리의 다른 글
[Spring] **23. 개념 보충 공부(2) (0) | 2024.08.12 |
---|---|
[Springboot] 22. 계좌 상세 보기 기능(2단계-화면 생성) (0) | 2024.08.12 |
[Springboot] 20. 입금 기능 만들기 (0) | 2024.08.08 |
[Springboot] 19. 출금 기능 만들기 (0) | 2024.08.08 |
[SpringBoot] 18. 중간 리팩토링 (0) | 2024.08.08 |