
- 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 find a value in a set of values separated by comma in a custom variable
For this, use FIND_IN_SET() in MySQL and use the value from a custom variable. Let us first create a −
mysql> create table DemoTable1411 -> ( -> Value int -> ) -> ; Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1411 values(10); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1411 values(50); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1411 values(60); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select −
mysql> select * from DemoTable1411;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 50 | | 60 | +-------+ 3 rows in set (0.00 sec)
Following is the query to find a value in set of other values. Here, we have set a custom variable first, which is used for the FIND_IN_SET() method −
mysql> set @setOfValues:='70,50,100'; Query OK, 0 rows affected (0.00 sec) mysql> select * from DemoTable1411 -> where find_in_set(Value,@setOfValues);
This will produce the following output −
+-------+ | Value | +-------+ | 50 | +-------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to get a single value from position of comma-separated string?
- MySQL query to select a row which contains same number in a column with set of numbers separated by comma?
- How to set a comma separated list as a table in MySQL?
- Set MySQL select in a custom variable
- Find specific records from a column with comma separated values in MySQL
- Display all the column values in a single row separated by comma in MySQL?
- MySQL query to search between comma separated values within one field?
- Check if value exists in a comma separated list in MySQL?
- Set the result of a query to a variable in MySQL?
- Set a custom value for NULL or empty values in MySQL
- Find integer in text data (comma separated values) with MySQL?
- MySQL query to retrieve records from the part of a comma-separated list?
- How can I search within a table of comma-separated values in MySQL?
- Convert a Set of String to a comma separated String in Java
- How can I match a comma separated list against a value in MySQL?

Advertisements