How select specific rows in MySQL?

MySQLMySQLi Database

To select specific rows, use FIND_IN_SET() function in MySQL. Let us first create a table −

mysql> create table DemoTable
   (
   ListOfValues varchar(200)
   );
Query OK, 0 rows affected (0.31 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('112,114,567,Java,345');
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable values('222,214,256');
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable values('2,567,98,C');
Query OK, 1 row affected (0.06 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------------------+
| ListOfValues         |
+----------------------+
| 112,114,567,Java,345 |
| 222,214,256          |
| 2,567,98,C           |
+----------------------+
3 rows in set (0.00 sec)

Following is the query to select specific rows in MySQL −

mysql> select *from DemoTable where find_in_set('2',ListOfValues) >0;

This will produce the following output −

+--------------+
| ListOfValues |
+--------------+
| 2,567,98,C   |
+--------------+
1 row in set (0.03 sec)
raja
Published on 20-May-2019 16:27:18
Advertisements