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

MySQL에서 WHERE 절을 사용하여 쿼리에 배열을 전달하시겠습니까?

<시간/>

where IN 절의 도움으로 배열을 전달할 수 있습니다. 먼저 이 예를 위해 새 테이블을 생성하겠습니다.

mysql> create table PassingAnArrayDemo
   -> (
   -> id int,
   -> Firstname varchar(100)
   -> );
Query OK, 0 rows affected (1.60 sec)

이제 레코드를 삽입해 보겠습니다.

mysql> insert into PassingAnArrayDemo values(1,'John'),(2,'Carol'),(3,'Smith'),(4,'Bob'),(5,'Johnson'),(6,'David'),(7,'Sam'),(8,'Jessica');
Query OK, 8 rows affected (0.32 sec)
Records: 8  Duplicates: 0  Warnings: 0

모든 기록을 표시합니다.

mysql> select *from PassingAnArrayDemo;

다음은 출력입니다.

+------+-----------+
| id   | Firstname |
+------+-----------+
|    1 | John      |
|    2 | Carol     |
|    3 | Smith     |
|    4 | Bob       |
|    5 | Johnson   |
|    6 | David     |
|    7 | Sam       |
|    8 | Jessica   |
+------+-----------+
8 rows in set (0.00 sec)

다음은 where IN 절의 도움으로 배열 매개변수를 보내는 구문입니다.

mysql> SELECT *
   -> FROM PassingAnArrayDemo where id IN(1,3,6);

다음은 출력입니다.

+------+-----------+
| id   | Firstname |
+------+-----------+
|    1 | John      |
|    3 | Smith     |
|    6 | David     |
+------+-----------+
3 rows in set (0.04 sec)