select name, count(*) from users
group by name
order by count(*) desc
name으로 묶고 묶인 name과 그 수를 나타내기
order by는 기본적으로 asc(오름차순)으로 정렬된다.
별칭 Alias 쿼리가 길어지면 알아보기 힘들기 때문에 별칭을 붙여준다.
Join

Left Join, Inner Join 위주로 학습
+)Left Join은 순서가 중요하다!
3-6 Q2
날짜 이후 표시
SELECT c.title, chk.week, count(*) CNT FROM courses c
inner join checkins chk on c.course_id = chk.course_id
inner join orders o on chk.user_id = o.user_id
WHERE o.created_at >= '2020-08-01'
group by c.title , chk.week
order BY c.title , chk.week
3-7 Q1
count()는 Null 값을 세지 않는다!
Round(숫자, 자리수)
SELECT count(pu.point_user_id) as pucnt, count(*) as cnt, round((count(pu.point_user_id)/count(*)),2) as ratio FROM users u
left join point_users pu on u.user_id = pu.user_id
where u.created_at BETWEEN '2020-07-10' and '2020-07-20'
2주차 숙제 복습을 하고 3주차 타임라인 메모 사이트 서버 만드는 부분까지 진행했다.
CRUD API를 그래도 서너번은 구현해 보니까 이러이러한 순서로 진행해야겠구나 정도의 느낌은 잡혔다.
그러나 Lombok을 쓰면서 드는 생각이, 너무 여기에 의존하게 되는 게 아닐까 싶어서 좀 무섭기도 하다.
아직까지는 답안 코드와 오가면서 진행을 해도 실행이 바로 안되고 오류가 생기는데, 더 익숙해져서 보지 않고도 전체 다 작성할 수 있도록 되면 좋겠다.
domain.MemoRepository.java
public interface MemoRepository extends JpaRepository<Memo, Long> {
List<Memo> findAllByOrderByModifiedAtDesc(); // 수정 시간 기준 최신순으로 정렬
}
4가지 API작성 - Controller
JPA레포 상속, 추가 정렬메소드 등 - Repository
받아오는 표 칼럼이나 기본키 등 설정, 완충재 받아주고 업데이트 메소드 보충 - Memo
update 메소드 - Service
완충재 + 생성자도 - RequestDto
오늘은 실습이 꽤 많았다. 기본 for문과 while문 복습을 거치고 튜터님이 내주신 문제를 푸는 방식으로 진행되었다.
예외처리 연습도 할 겸, 조건이 주어지면 맞지 않을 때 예외 설정을 해주면서 진행했다.
//내 코드
Scanner sc = new Scanner(System.in);
System.out.println("자연수를 입력하세요: ");
int a = sc.nextInt();
int sum =0;
if (a<=0) throw new Exception("자연수가 아닙니다.");
try {
for (int i = 0; i < a; i++) {
if ((i + 1) % 2 == 0) {
sum += (i + 1);
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(sum);
하고 나서 생각해보니 문자면 어떡하지 싶었다;;
그리고 예외 발생 시 반복문 빠져나오지 말고 재시도하게끔 하고 싶었다.
일단 다음 문제로 넘어가서...
//답안 코드
Scanner sc=new Scanner(System.in);
System.out.println("2~9 사이에 수를 입력하세요.");
int n=sc.nextInt();
while(true){
if(n< 2||n>9){
System.out.println("2~9사이의 수를 입력하세요. ");
n=scanner.nextInt();
}else{
break;
}
}
for (int i = 1; i < 10; i++) {
System.out.println(n + "*" + i + " = " + (n*i));
//내 코드
System.out.println("2~9 사이의 수를 입력하세요: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n>=2 && n<=9){
System.out.println(n+"단");
for (int j = 1; j < 10; j++) {
System.out.println(n + " * " + j + " = " + (n * j));
}
} else {
System.out.println("2~9 사이의 수가 아닙니다.");
}
//답안 코드
while(true){
System.out.println("다음 중 프로그래밍 언어가 아닌것은?");
System.out.println("1. Javascript\n2. Java\n3.스파르타\n4. HTML\n0. 나가기");
num=sc.nextInt();
if(num==3){
System.out.println("정답입니다.");
}else if(num==1||num==2||num==4){
System.out.println("오답입니다.");
}else if(num==0){
System.out.println("안녕히 가세요.");
break;
}else{
System.out.println("잘못 입력 하셨습니다. ");
}
}
//내 코드
System.out.println("다음 중 프로그래밍 언어가 아닌 것은?");
System.out.println("1.jsp\t2.java\t3.스파르파\t4.HTML\t5.나가기");
Scanner sc = new Scanner(System.in);
while (true) {
try {
int n = sc.nextInt();
if (n == 3) {
System.out.println("정답입니다.");
break;
} else if (n == 1 || n == 2 || n == 4) {
System.out.println("오답입니다.");
sc.nextLine();
continue;
} else if (n == 5) {
System.out.println("안녕히 가세요~");
break;
} else {
System.out.println("잘못 입력했습니다.");
sc.nextLine();
continue;
}
} catch (InputMismatchException e) {
System.out.println("잘못 입력했습니다.");
sc.nextLine();
continue;
}
}
//내 코드
int even = 0;
int odd = 0;
System.out.println("자연수 10개를 입력해 주세요.");
Scanner sc1 = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
try {
int a = sc1.nextInt();
if (a <= 0) throw new Exception("자연수를 입력해 주세요.");
if (a % 2 == 0)
even++;
else odd++;
} catch (InputMismatchException e) {
System.out.println("자연수를 입력해 주세요.");
sc1.nextLine(); // 스캐너 비워주기
i--;
continue;
} catch (Exception e) {
System.out.println(e.getMessage());
i--;
continue;
}
}
System.out.println("짝수의 개수: " + even + ", 홀수의 개수: " + odd);
문자 입력했을 때 Input~ 예외를 설정해주면 된다고 해서 따로 설정해주고 스캐너를 비워줬다.
이런 오류가 적게 하려면 nextLine으로 받아서 정수로 파싱하면 좋다는데 그것도 다음에 해봐야겠다. 다음에..ㅎㅎ
for문과 while문을 다시 한 번 짚고 갈 수 있어서 좋았다!