DB/명령프롬프트 - mysql

[MySQL] Database 검색 (and/or, like, between, in())

congs 2023. 3. 27. 09:05

전체출력, 자료검색,  and / or 

  • 전체출력 : select * from 검색하고자하는 테이블 명
mysql> select*from student;
+------+--------+------+---------+----------+-------+
| num  | name   | age  | address | major1   | score |
+------+--------+------+---------+----------+-------+
| 1111 | 홍길동 |   23 | seoul   | computer |    89 |
| 2222 | 강길순 |   24 | seoul   | English  |    87 |
| 3333 | 이순신 |   22 | Inchen  | English  |    57 |
| 4444 | 강감찬 |   23 | Inchen  | English  |    67 |
| 5555 | 유관순 |   21 | suwon   | Computer |    97 |
+------+--------+------+---------+----------+-------+
5 rows in set (0.00 sec)

/* major1이 'computer'인 자료 검색 */
mysql> select*from student where major1='computer';
+------+--------+------+---------+----------+-------+
| num  | name   | age  | address | major1   | score |
+------+--------+------+---------+----------+-------+
| 1111 | 홍길동 |   23 | seoul   | computer |    89 |
| 5555 | 유관순 |   21 | suwon   | Computer |    97 |
+------+--------+------+---------+----------+-------+
2 rows in set (0.00 sec)

< and 사용 >
/* major1이 'computer'이고 score이 90점 이상인 학생의 이름 검색 */
mysql> select name from student where major1='computer' and score>=90;
+--------+
| name   |
+--------+
| 유관순 |
+--------+
1 row in set (0.00 sec)

< or사용 >
/* seoul이나 inchen에 사는 사람 검색 */
mysql> select * from student where address='seuol' or address='inchen';
+------+--------+------+---------+---------+-------+
| num  | name   | age  | address | major1  | score |
+------+--------+------+---------+---------+-------+
| 3333 | 이순신 |   22 | Inchen  | English |    57 |
| 4444 | 강감찬 |   23 | Inchen  | English |    67 |
+------+--------+------+---------+---------+-------+
2 rows in set (0.00 sec)

원하는 정보만 출력 like , s% 

: 단어를 포함하는 값을 조건으로 줄 때 like (% : 포함하는 값 추출)

< s로 시작하는 도시에 살고 있는 데이터 추출 >
- 단어를 포함하는 값을 조건으로 줄 때 like
- % 다른것을 포함할 수 있다는 의미

mysql> select * from student
    -> where address like 's%';
+------+--------+------+---------+----------+-------+
| num  | name   | age  | address | major    | score |
+------+--------+------+---------+----------+-------+
| 1111 | 홍길동 |   23 | seoul   | computer |    89 |
| 2222 | 강길순 |   24 | seoul   | English  |    87 |
| 5555 | 유관순 |   21 | suwon   | Computer |    97 |
| 9999 | 다래뇽 |   22 | seoul   | computer |    70 |
+------+--------+------+---------+----------+-------+
4 rows in set (0.00 sec)

< s가 포함된 도시에 살고 있는 데이터 추출 >
mysql> select * from student
    -> where address like '%s%';
+------+--------+------+---------+----------+-------+
| num  | name   | age  | address | major    | score |
+------+--------+------+---------+----------+-------+
| 1111 | 홍길동 |   23 | seoul   | computer |    89 |
| 2222 | 강길순 |   24 | seoul   | English  |    87 |
| 5555 | 유관순 |   21 | suwon   | Computer |    97 |
| 9999 | 다래뇽 |   22 | seoul   | computer |    70 |
+------+--------+------+---------+----------+-------+
4 rows in set (0.00 sec)

 

숫자의 범위를 찾을 때  between a and b / in()

< 숫자의 범위를 찾을 때 between a and b >

/* 점수가 70~90 사이인 자료 검색 */
mysql> select * from student where score between 70 and 90;
+------+--------+------+---------+----------+-------+
| num  | name   | age  | address | major1   | score |
+------+--------+------+---------+----------+-------+
| 1111 | 홍길동 |   23 | seoul   | computer |    89 |
| 2222 | 강길순 |   24 | seoul   | English  |    87 |
+------+--------+------+---------+----------+-------+
2 rows in set (0.00 sec)

< in() >

/* in()을 이용하여 seoul, inchen에 사는 사람 검색 */
mysql> select * from student where address in('seuol', 'inchen');
+------+--------+------+---------+---------+-------+
| num  | name   | age  | address | major1  | score |
+------+--------+------+---------+---------+-------+
| 3333 | 이순신 |   22 | Inchen  | English |    57 |
| 4444 | 강감찬 |   23 | Inchen  | English |    67 |
+------+--------+------+---------+---------+-------+
2 rows in set (0.00 sec)