How MySQL FIELD() and ELT() functions are complements of each other?

MySQLMySQLi Database

On the basis of the working of both the functions, we can say that both are the complement of each other. Actually, as we know that FIELD() function, on providing a string as an argument, returns the index number of the string from string list and ELT() function, on providing index number as an argument, returns the string from string list. In the following example, we have applied both the functions on the same string, it would demonstrate the concept −

Example

mysql> SELECT ELT(4, 'Ram','is','good','boy')As Result;

+--------+
| Result |
+--------+
| boy    |
+--------+

1 row in set (0.00 sec)

mysql> SELECT FIELD('boy', 'Ram','is','good','boy')As Result;

+--------+
| Result |
+--------+
|      4 |
+--------+

1 row in set (0.00 sec)

As we can see from the above result set that one is getting index number as an argument to give the string as output and another is taking a string as an argument to give the index number as output. Hence, they both are the complement of each other.

raja
Published on 20-Feb-2018 06:56:01
Advertisements