무작위로 선택하려면 ORDER BY RAND()를 사용하십시오. 2개의 값만 선택하려면 MySQL에서 LIMIT 2를 사용하십시오. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1815 ( Question text ); Query OK, 0 rows affected (0.00 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable1815 values('What is your name?'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1815 values('What is your college name?'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1815 values('What is your nick name?'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1815 values('What is your enemy name?'); Query OK, 1 row affected (0.00 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable1815;
이것은 다음과 같은 출력을 생성합니다 -
+----------------------------+ | Question | +----------------------------+ | What is your name? | | What is your college name? | | What is your nick name? | | What is your enemy name? | +----------------------------+ 4 rows in set (0.00 sec)
다음은 2개의 레코드만 무작위로 선택하는 쿼리입니다 -
mysql> select * from DemoTable1815 order by rand() limit 2;
이것은 다음과 같은 출력을 생성합니다 -
+----------------------------+ | Question | +----------------------------+ | What is your college name? | | What is your enemy name? | +----------------------------+ 2 rows in set (0.00 sec)