How to set a comma separated list as a table in MySQL?

MySQLMySQLi Database

You can use UNION ALL for this.

Let us get list 10, 20, 30, 40, 50 as a table with UNION ALL −

mysql> select 10 Number UNION ALL select 20 Number UNION ALL select 30 Number 
   UNION ALL select 40 Number UNION ALL select 50 Number;

Output

+--------+
| Number |
+--------+
| 10     |
| 20     |
| 30     |
| 40     |
| 50     |
+--------+
5 rows in set (0.00 sec)

Let us see another example. To get the list 1,2,3 as a table, use the below query −

mysql> SELECT 1 a UNION ALL SELECT 2 a UNION ALL SELECT 3 a;

Output

+---+
| a |
+---+
| 1 |
| 2 |
| 3 |
+---+
3 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 16:05:36
Advertisements