
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fetch random rows in MySQL with comma separated values?
To fetch random rows in MySQL, use ORDER BY RAND(). Let us first create a table −
mysql> create table DemoTable1835 ( ListOfIds varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1835 values('10,20,30'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1835 values('70,80,90'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1835 values('45,67,89'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1835 values('98,96,49'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1835;
This will produce the following output −
+-----------+ | ListOfIds | +-----------+ | 10,20,30 | | 70,80,90 | | 45,67,89 | | 98,96,49 | +-----------+ 4 rows in set (0.00 sec)
Here is the query to fetch random rows in MySQL
mysql> select * from DemoTable1835 where ListOfIds NOT IN(10,20,70) order by rand() limit 2;
This will produce the following output −
+-----------+ | ListOfIds | +-----------+ | 98,96,49 | | 45,67,89 | +-----------+ 2 rows in set, 4 warnings (0.00 sec)
- Related Questions & Answers
- Fetch records from comma separated values using MySQL IN()?
- MySQL query to fetch specific records matched from an array (comma separated values)
- Fetch random rows from a table with MySQL
- Find integer in text data (comma separated values) with MySQL?
- Count values from comma-separated field in MySQL?
- Get values from all rows and display it a single row separated by comma with MySQL
- How to include quotes in comma separated column with MySQL?
- How to search for particular strings between comma separated values in MySQL?
- Find specific records from a column with comma separated values in MySQL
- MySQL query to search between comma separated values within one field?
- How to split comma separated values in an R vector?
- How to sum a comma separated string (string with numbers) in MySQL?
- How to count specific comma separated values in a row retrieved from MySQL database?
- How can I search within a table of comma-separated values in MySQL?
- Remove specific word in a comma separated string with MySQL
Advertisements