MySQL/ SELECT
-
테이블로부터 데이터를 조회하는 명령어
-
*는 모든 column
// 기본 문법SELECT column1, column2, ...FROM table_name;SELECT *FROM table_name;
- DISTINCT 키워드를 사용하면 중복을 제거하여 조회
// DISTINCTSELECT DISTINCT columnFROM table_name
- WHERE 키워드를 사용하여 조건에 맞는 데이터만 조회 가능
// WHERESELECT column1, column2, ...FROM table_nameWHERE conditionSELECT * FROM CustomersWHERE CustomerID = 1;
- AND, OR, NOT으로 조건을 조합할 수 있음
// AND, OR, NOTSELECT column1, column2FROM table_nameWHERE condition1 AND codition2 AND condition3;SELECT column1, column2FROM table_nameWHERE condition1 OR codition2 OR condition3;SELECT column1, column2FROM table_nameWHERE NOT condition;// Country는 Germany이고, City는 Berlin인 데이터SELECT *FROM CustomersWHERE Country='Germany' AND City='Berlin';// City가 Berlin 또는 Stuttgart인 데이터SELECT *FROM CustomersWHERE City='Berlin' OR City='Stuttgart';// Country가 Germany 또는 Spain인 데이터SELECT *FROM CustomersWHERE Country='Germany' OR Country='Spain';// Country가 Germany가 아닌 데이터SELECT *FROM CustomersWHERE NOT Country='Germany';// Country는 Germany이고 City는 Berlin 또는 Stuttgart인 데이터SELECT *FROM CustomersWHERE Country='Germany' AND (City='Berlin' OR City='Stuttgart');// Country가 Germany도 아니고 USA도 아닌 데이터SELECT *FROM CustomersWHERE NOT Country='Germany' AND NOT Country='USA';
-
ORDER BY로 정렬 기준 설정
-
ASC는 오름차순, DESC는 내림차순
// ORDER BYSELECT column1, column2, ...FROM table_nameORDER BY column1, column2, ... ASC|DESC;// Country 기준으로 정렬SELECT *FROM CustomersORDER BY Country;// Country 기준으로 내림차순 정렬SELECT *FROM CustomersORDER BY Country DESC;// Country로 먼저 정렬, Country가 같은 경우 CustomerName으로 정렬SELECT *FROM CustomersORDER BY Country, CustomerName;// Country로는 오름차순으로, CustomerName으로는 내림차순으로 정렬SELECT *FROM CustomersORDER BY Country ASC, CustomerName DESC;