MySQL | ELT method
Start your free 7-days trial now!
MySQL's ELT(~)
method returns the N
th element from the list of provided strings.
Parameters
1. N
| integer
A number specifying which element to return from the list of input strings.
2. str
| string
A list of strings (i.e. strings separated by commas).
Return value
The N
th element from the list of provided strings.
Examples
Consider the following table about some students:
student_id | fname | lname | day_enrolled | age | username |
---|---|---|---|---|---|
1 | Sky | Towner | 2015-12-03 | 17 | stowner1 |
2 | Ben | Davis | 2016-04-20 | 19 | bdavis2 |
3 | Travis | Apple | 2018-08-14 | 18 | tapple3 |
4 | Arthur | David | 2016-04-01 | 16 | adavid4 |
5 | Benjamin | Town | 2014-01-01 | 17 | btown5 |
The above sample table can be created using the code here.
Basic usage
To return the 4th string in the list:
SELECT ELT(4, 'I', 'am', 'going', 'to', 'the', 'park');
+-------------------------------------------------+| ELT(4, 'I', 'am', 'going', 'to', 'the', 'park') |+-------------------------------------------------+| to |+-------------------------------------------------+
Here, "to"
is returned since it is the 4th element.
Columns
To return the values of the second column in the list:
SELECT ELT(2, fname, lname, username)FROM students;
+--------------------------------+| ELT(2, fname, lname, username) |+--------------------------------+| Towner || Davis || Apple || David || Town |+--------------------------------+
The above yields the same results as:
SELECT lname FROM students;