This tutorial explains how to use the MySQL FIELD function with syntax and examples.
The MySQL FIELD function returns the position of a value in a list of values (val1, val2, val3, ...).
The syntax for the FIELD function in MySQL is:
FIELD( value, val1, val2, val3, ... )
The value to find in the list.
The list of values that is to be searched.
The FIELD function can be used in the following versions of MySQL:
Let's look at some MySQL FIELD function examples and explore how to use the FIELD function in MySQL.
For example:
mysql> SELECT FIELD('b', 'a', 'b', 'c', 'd', 'e', 'f');
Output: 2
mysql> SELECT FIELD('B', 'a', 'b', 'c', 'd', 'e', 'f');
Output: 2
mysql> SELECT FIELD(15, 10, 20, 15, 40);
Output: 3
mysql> SELECT FIELD('c', 'a', 'b');
Output: 0
mysql> SELECT FIELD('g', '');
Output: 0
mysql> SELECT FIELD(null, 'a', 'b', 'c');
Output: 0
mysql> SELECT FIELD('a', null);
Output: 0