해당 작업 이어서 상세 버튼 클릭 -> 포인트,상품구매리스트도 출력하기 (points,mall_product 테이블)
⚡ 상세 table에 포인트 추가
<%
String idx = request.getParameter("idx");
String sql = "select * from mall_member where midx=?";
PreparedStatement ps = dbcon.prepareStatement(sql);
ps.setString(1,idx);
ResultSet rs = ps.executeQuery();
rs.next();
//point 테이블을 select하여 해당 id에 맞는 포인트 합계 출력하는 mysql문법
String sql2 = "select sum(mall_point) as total from points where mall_id=?";
ps = dbcon.prepareStatement(sql2);
ps.setString(1, rs.getString("mall_id"));
ResultSet rs2 = ps.executeQuery();
rs2 = ps.executeQuery();
rs2.next();
%>
~생략
<tr>
<td>포인트(적립금)</td>
//<td><%= rs2.getString("total") %></td> //이렇게해도되고
<td> //이렇게하면 ,찍힌 값 출력
<%
DecimalFormat df = new DecimalFormat("###,###");
out.print(df.format(rs2.getInt("total")));
%>
</td>
<tr>
DecimalFormat : , 찍는 라이브러리
=> 실무에선 join or view를 사용한다 ! 이렇게하면 막코드👎
🔽 이걸 join으로 한다면?
sql 문법 :
select a.*,sum(b.mall_point) as total from mall_member as a join points as b where a.midx='7' and a.mall_id=b.mall_id group by b.mall_id;
join시 어떤 table의 colomn인지 명세를 꼭 해줘야함 (*도)
group by : 같은 그룹끼리 묶어라
cmd로 확인 후 나오면 작업해라!
⚡ join방식으로 point 출력
<%
String idx = request.getParameter("idx");
String sql = "select a.*,sum(b.mall_point) as total from mall_member as a join points as b where a.midx=? and a.mall_id=b.mall_id group by b.mall_id";
PreparedStatement ps = dbcon.prepareStatement(sql);
ps.setString(1,idx);
ResultSet rs = ps.executeQuery();
rs.next();
%>
~생략
<td>
<%
DecimalFormat df = new DecimalFormat("###,###");
out.print(df.format(rs.getInt("total")));
%>
</td>
🔽 view table 방식으로 한다면?
cmd로 view table을 사전에 만들고 작업하는 방식!
⚡ view 방식으로 point 출력
create view member_info as (select a.*,sum(b.mall_point) as total
from mall_member as a join points as b
where a.mall_id=b.mall_id group by b.mall_id);
이걸로 cmd로 view table 생성하고
String sql = "select * from member_info where midx=?";