3주차 SQL(흐리멍텅)

승은·2022년 12월 12일
0

try_sql

목록 보기
4/4

약간 흐려지기 시작했당.. 이게 몬 3일 쉬고 오니까 하나도 기억안난다.. 더듬어 더듬어 적어봄미당..(머쓱타드)

  • Join: 여러 테이블을 연결할때 사용(vlookup 가튼거)
    두개의 테이블을 한눈에 볼 때!(두 정보를 연결해서 보고파)

    1. left join (그냥 연결..왼쪽옆에다가 붙인다는 의미인듯)
    • ex) select * from enrolleds e
      left join enrolleds_detail ed on e.enrolleds_id = ed.enrolleds id
    1. inner join(합집합)

      그래서 이너조인이 더 어렵다는데

    • ex) select * from users u
      inner join orders o on u.user_id=o.user_id

      ** alias: 테이블이 많아지면서 필드명과 테이블명이 헷갈려 실수할 수 있는데, 이렇게 alias를 지정해 주면 편하고 깔끔하게 SQL 쿼리를 작성할 수 있엄. > count(u.user_id) as cnt

      **유저 중에, 포인트가 없는 사람(=즉, 시작하지 않은 사람들)의 통계는 is NULL , is not NULL로 구분!

  • Union: 결과물 합치기

    • ex) (
      select '7월' as month, c.title, c2.week, count() as cnt from checkins c2
      inner join courses c on c2.course_id = c.course_id
      inner join orders o on o.user_id = c2.user_id
      where o.created_at < '2020-08-01'
      group by c2.course_id, c2.week
      order by c2.course_id, c2.week
      )
      union all
      (
      select '8월' as month, c.title, c2.week, count(
      ) as cnt from checkins c2
      inner join courses c on c2.course_id = c.course_id
      inner join orders o on o.user_id = c2.user_id
      where o.created_at > '2020-08-01'
      group by c2.course_id, c2.week
      order by c2.course_id, c2.week
      )

**근데 얘네는 order by 안먹혀서 SubQuery적용 해줘야함

지금 보면서도 헷갈리는 상태라서 다시한번 복습요망..

profile
빅데이터를 배우자🥹

0개의 댓글