FIELD

FIELD is a MySQL function that returns the index (position) of a specified value in a list of values. The search is case-sensitive. If the value is not found, the function returns 0.

FIELD(str,str1,str2,str3,…)

  • str: This parameter is the string to be compared. It is the value that the function is searching for within the subsequent parameters.
  • str1, str2, str3,…: These parameters are the set of strings where “str” is being searched for. The function returns the index (position) of the first occurrence of “str” in this list. If “str” is not found, the function returns 0. Multiple strings can be provided.

Example

SELECT FIELD('MySQL', 'Oracle', 'SQL Server', 'MySQL', 'MongoDB');

Output

3

Explanation

The FIELD function in MySQL returns the index (position) of a string within a list of strings. It returns 0 if the string is not found. In the provided example, ‘MySQL’ is the third string in the list, so the output is 3.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.