
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to get string from one column and find its position in another column with comma separated values?
For this, use FIND_IN_SET(). Let us first create a table −
mysql> create table DemoTable1866 ( Value1 int, ListOfValues varchar(100) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1866 values(56,'78,56,98,95'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1866 values(103,'103,90,102,104'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1866 values(77,'34,45,77,78'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1866;
This will produce the following output −
+--------+----------------+ | Value1 | ListOfValues | +--------+----------------+ | 56 | 78,56,98,95 | | 103 | 103,90,102,104 | | 77 | 34,45,77,78 | +--------+----------------+ 3 rows in set (0.00 sec)
Here is the query to get string position in another column −
mysql> select Value1,ListOfValues,find_in_set(Value1,ListOfValues) as Output from DemoTable1866;
This will produce the following output −
+--------+----------------+--------+ | Value1 | ListOfValues | Output | +--------+----------------+--------+ | 56 | 78,56,98,95 | 2 | | 103 | 103,90,102,104 | 1 | | 77 | 34,45,77,78 | 3 | +--------+----------------+--------+ 3 rows in set (0.00 sec)
- Related Articles
- Find specific records from a column with comma separated values in MySQL
- MySQL query to get a single value from position of comma-separated string?
- Searching from a comma separated MySQL column?
- How to include quotes in comma separated column with MySQL?
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- MySQL query to search between comma separated values within one field?
- How to display data from a MySQL column separated by comma?
- Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?
- MySQL query to fetch specific records matched from an array (comma separated values)
- Display all the column values in a single row separated by comma in MySQL?
- Copy values of one column to another using INSERT and SELECT in a single MySQL query
- Find integer in text data (comma separated values) with MySQL?
- MySQL query to group by column and display the sum of similar values in another column
- MySQL query to replace backslash from a varchar column with preceding backslash string values
- MySQL query to remove string from a column with values EMP1, EMP2, EMP3, etc.

Advertisements