Programmers/SQL(34)
-
[mysql/oracle] 자동차 종류 별 특정 옵션이 포함된 자동차 수 구하기
문제 결과 mysql / oracle 1 2 3 4 5 SELECT car_type, count(*) cars from car_rental_company_car where options like '%시트%' group by car_type order by car_type cs 공교롭게도 요구한 통풍시트, 열선시트, 가죽시트 모두 '시트'가 들어가서 like와 와일드카드로 처리했다. 정규식으로도 가능하다. 1 2 3 4 5 SELECT car_type, count(*) cars from car_rental_company_car where options regexp '시트' group by car_type order by car_type cs 정규식 검색 . : 문자 하나 * : 앞에 나온 문자의 0개 이상..
2023.01.29 -
[mysql/oracle] 카테고리 별 상품 개수 구하기
문제 결과 mysql / oracle substr 1 2 3 4 5 SELECT substr(product_code,1,2) category , count(*) products from product group by substr(product_code,1,2) order by category cs 상품코드의 앞 2자리가 카테고리 코드이므로 2글자만 가져오기 위해 substr을 이용하였다. substr(문자열 or 컬럼명, 몇 번째 글자부터, 몇 글자나 자를 것인가) mysql left 1 2 3 4 5 SELECT left(product_code,2) category , count(*) products from product group by left(product_code,2) order by categ..
2023.01.29 -
[mysql/oracle] 중성화 여부 파악하기
문제 결과 mysql / oracle case문 1 2 3 4 5 6 7 8 9 SELECT animal_id , name , (case when sex_upon_intake like '%Spayed%' or sex_upon_intake like '%Neutered%' then 'O' else 'X' end) 중성화 from animal_ins order by animal_id Colored by Color Scripter cs like 덕분에 와일드카드를 이용할 수 있다. mysql if문 1 2 3 4 5 6 SELECT animal_id , name , if ( sex_upon_intake like '%Spayed%' or sex_upon_intake like '%Neutered%' , 'O', '..
2023.01.29 -
[mysql/oracle] 이름에 el이 들어가는 동물 찾기
문제 결과 mysql / oracle 1 2 3 4 SELECT animal_id, name from animal_ins where lower(name) like lower('%el%') and animal_type = 'Dog' order by name Colored by Color Scripter cs 이름에 'el'이 들어가는 강아지를 찾는 문제인데 관건은 대소문자 구분없이 이다. 대소문자 구분을 없애려면 모든 name을 대문자 or 소문자로 바꿔주는 것이다. 대문자로 바꾸기 - upper 소문자로 바꾸기 - lower 위 코드에서 upper을 사용해도 같은 결과를 낼 수 있다. upper, lower와 늘 같이 나오는 게 initcap인데 이는 맨 앞 글자를 대문자로 바꾸는 것이기 때문에 이 문제..
2023.01.28 -
[mysql/oracle] 강원도에 위치한 생산공장 목록 출력하기
문제 결과 mysql / oracle 1 2 3 4 SELECT factory_id, factory_name, address from food_factory where address like '강원도%' order by factory_id cs 강원도에 위치한 공장을 찾기 위해서는 주소가 강원도로 시작한다는 조건을 걸면 된다. 문자열 찾는 방법 2023.01.21 - [DB] - 특정 문자열 포함 여부 확인하기 특정 문자열 포함 여부 확인하기 주소에 경기도가 들어가는 row를 출력해보자 like 문자열의 패턴을 찾는 함수이다. like는 와일드 카드와 함께 사용할 수 있다 Wildcard % : 0개 이상의 문자 _ : 하나당 1개의 문자 ex) % 1 2 3 SELECT WAREHOUSE codelab..
2023.01.28 -
[mysql/oracle] 이름이 없는 동물의 아이디
문제 결과 mysql/oracle 1 2 3 4 SELECT ANIMAL_ID FROM ANIMAL_INS WHERE NAME IS NULL ORDER BY ANIMAL_ID cs
2023.01.28