
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Select first element of a commaseparated list in MySQL?
To select first element of a comma-separated list, you can use SUBSTRING_INDEX(). Let us first create a table:
mysql> create table DemoTable ( CSV_Value varchar(200) ); Query OK, 0 rows affected (0.81 sec)
Following is the query to insert some records in the table using insert command. We have inserted records in the form of comma-separated integer list:
mysql> insert into DemoTable values('10,20,50,80'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('100,21,51,43'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('1,56,103,1090'); Query OK, 1 row affected (0.26 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+---------------+ | CSV_Value | +---------------+ | 10,20,50,80 | | 100,21,51,43 | | 1,56,103,1090 | +---------------+ 3 rows in set (0.00 sec)
Following is the query to select first element of a comma-separated list:
mysql> select SUBSTRING_INDEX(CSV_Value,',',1) AS FIRST_ELEMENT from DemoTable;
This will produce the following output. First element of every list is displayed here:
+---------------+ | FIRST_ELEMENT | +---------------+ | 10 | | 100 | | 1 | +---------------+ 3 rows in set (0.03 sec)
- Related Articles
- Select first word in a MySQL query?
- Select results from the middle of a sorted list in MySQL?
- Select random number from a specific list in MySQL?
- How to select a random element from a C# list?
- Move first element to end of a given Linked List in C++
- Select all except the first character in a string in MySQL?
- MySQL Select where values IN List string?
- How to select first 10 elements from a MySQL database?
- How to get the first element of the List in Java?
- MySQL SELECT to skip first N results?\n
- Get first element with maximum value in list of tuples in Python
- How to pop the first element from a C# List?
- How to write a MySQL query to select first 10 records?
- How to extract only first sub-element from a list in R?
- Output only the first word from select list value - jQuery?

Advertisements