FIELD() FUNCTION in MySQL

FIELD() FUNCTION
The MySQL FIELD function is used to get the position of a value in a list of values. The various versions of MySQL support the FIELD function, namely, MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0 and MySQL 3.23.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
FIELD( value, val_1, val_2, ... val_n )
FIELD( value, val_1, val_2, ... val_n )
FIELD( value, val_1, val_2, ... val_n )

Parameters:
value: It is used to specify the value to search.
val_1, val_2, … val_n: It is used to specify the values to be searched.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT FIELD ( ‘A’, ‘1’, ‘A’, ‘2’, ‘B’, ‘3’, ‘C’ );
mysql> SELECT FIELD ( ‘A’, ‘1’, ‘A’, ‘2’, ‘B’, ‘3’, ‘C’ );
mysql> SELECT FIELD ( ‘A’, ‘1’, ‘A’, ‘2’, ‘B’, ‘3’, ‘C’ );

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
2
2
2

Explanation:
The value’s position is returned.