Computer >> 컴퓨터 >  >> 프로그램 작성 >> MySQL

MySQL은 여러 값을 선택하시겠습니까?


여러 값을 선택하려면 OR 및 IN 연산자와 함께 where 절을 사용할 수 있습니다.

구문은 다음과 같습니다 -

사례 1 - OR 사용

select *from yourTablename where yourColumnName = value1 or yourColumnName = value2 or yourColumnName = value3,.........N;

사례 2 - IN 사용

select *from yourTableName where yourColumnName IN(value1,value2,....N);

위의 구문을 이해하기 위해 테이블을 생성해 보겠습니다. 다음은 테이블을 생성하는 쿼리입니다 -

mysql> create table selectMultipleValues
−> (
−> BookId int,
−> BookName varchar(200)
−> );
Query OK, 0 rows affected (1.68 sec)

이제 insert 명령을 사용하여 테이블에 일부 레코드를 삽입할 수 있습니다. 레코드를 삽입하는 쿼리는 다음과 같습니다 -

mysql> insert into selectMultipleValues values(100,'Introduction to C');
Query OK, 1 row affected (0.18 sec)

mysql> insert into selectMultipleValues values(101,'Introduction to C++');
Query OK, 1 row affected (0.19 sec)

mysql> insert into selectMultipleValues values(103,'Introduction to java');
Query OK, 1 row affected (0.14 sec)

mysql> insert into selectMultipleValues values(104,'Introduction to Python');
Query OK, 1 row affected (0.14 sec)

mysql> insert into selectMultipleValues values(105,'Introduction to C#');
Query OK, 1 row affected (0.13 sec)

mysql> insert into selectMultipleValues values(106,'C in Depth');
Query OK, 1 row affected (0.15 sec)

select 문을 사용하여 테이블의 모든 레코드를 표시합니다. 쿼리는 다음과 같습니다 -

mysql> select *from selectMultipleValues;

다음은 출력입니다 -

+--------+------------------------+
| BookId | BookName               |
+--------+------------------------+
| 100    | Introduction to C      |
| 101    | Introduction to C++    |
| 103    | Introduction to java   |
| 104    | Introduction to Python |
| 105    | Introduction to C#     |  
| 106    | C in Depth             |
+--------+------------------------+
6 rows in set (0.00 sec)

다음은 OR 연산자를 사용하여 여러 값을 선택하는 쿼리입니다.

사례 1 - OR 연산자 사용

mysql> select *from selectMultipleValues where BookId = 104 or BookId = 106;

다음은 출력입니다 -

+--------+------------------------+
| BookId | BookName |
+--------+------------------------+
| 104 | Introduction to Python |
| 106 | C in Depth |
+--------+------------------------+
2 rows in set (0.00 sec)

사례 2 - In 연산자 사용

다음은 IN 연산자를 사용하여 여러 값을 선택하는 쿼리입니다.

mysql> select *from selectMultipleValues where BookId in(104,106);

다음은 출력입니다 -

+--------+------------------------+
| BookId | BookName               |
+--------+------------------------+
| 104    | Introduction to Python |
| 106    | C in Depth             |  
+--------+------------------------+
2 rows in set (0.00 sec)