deVSner

프로그래머스 sql - string, date 본문

자료구조 및 알고리즘/mysql

프로그래머스 sql - string, date

RudeofSun 2020. 6. 21. 10:07

 

1. 루시와 엘라 찾기

 

SELECT 
animal_id, 
name, 
sex_upon_intake 
from animal_ins 
where name in ('lucy', 'ella', 'pickle', 'rogan', 'sabrina', 'mitty') 
order by animal_id

 

in이 중요하다. 처음에 and로 했으나, 실패

 

https://www.w3schools.com/sql/sql_in.asp

 

SQL IN Operator

SQL IN Operator The SQL IN Operator The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions. IN Syntax SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ..

www.w3schools.com

 

 

2. 이름에 el이 들어가는 개 찾기

SELECT
animal_id,
name
from animal_ins
where name like '%el%' and animal_type = 'dog'
order by name

 

헷갈렸던 점은, '조건이 2개일 때', 라는 상황이었다.

구글에 검색하니까 WHERE는 and, or , not, in 과 조합할 수 있었다.

https://www.w3schools.com/sql/sql_where.asp

 

SQL WHERE Clause

SQL WHERE Clause The SQL WHERE Clause The WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified condition. WHERE Syntax SELECT column1, column2, ... FROM table_name WHERE condition; Note: Th

www.w3schools.com

https://www.w3schools.com/sql/sql_and_or.asp

 

SQL AND, OR, NOT Operators

SQL AND, OR and NOT Operators The SQL AND, OR and NOT Operators The WHERE clause can be combined with AND, OR, and NOT operators. The AND and OR operators are used to filter records based on more than one condition: The AND operator displays a record if al

www.w3schools.com

 

 

 

3. 중성화 여부 파악하기

SELECT
animal_id,name,
case when sex_upon_intake like '%Neutered%' or sex_upon_intake like '%Spayed%' then 'O'
else 'X'
end as '중성화'
from animal_ins

 

왠지 앞으로 case when 구문을 많이 쓸 거 같다는 예감...

활용적인 측면에서 case when을 설명하자면, 생성되어져 있는 칼럼에 대해서, 

내가 그것을 분석하고, 필요한 정보에 대해서 나만의 언어로 재가공할 수 있다는 점.