- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL randomly select 2 values from column values?
To randomly select, use ORDER BY RAND(). To select only 2 values, use LIMIT 2 in MySQL. Let us first create a table −
mysql> create table DemoTable1815 ( Question text ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
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)
Display all records from the table using select statement −
mysql> select * from DemoTable1815;
This will produce the following output −
+----------------------------+ | 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)
Here is the query to randomly select only 2 records −
mysql> select * from DemoTable1815 order by rand() limit 2;
This will produce the following output −
+----------------------------+ | Question | +----------------------------+ | What is your college name? | | What is your enemy name? | +----------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- How to select only non - numeric values from varchar column in MySQL?
- MYSQL select DISTINCT values from two columns?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?
- Select a specific value between two column values in MySQL?
- Select distinct values from three columns and display in a single column with MySQL
- MySQL Select Multiple VALUES?
- Select distinct values from two columns in MySQL?
- Select rows from a Pandas DataFrame based on column values
- MySQL select to count values equal to 0 and greater than 0 from a column?
- MySQL query to select column values ending with certain character/number?
- How to randomly replace values in an R data frame column?
- How to select different values from same column and display them in different columns with MySQL?
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- Add a character in the end to column values with MySQL SELECT?

Advertisements