Home  >  Article  >  Database  >  How to set comma separated list to table in MySQL?

How to set comma separated list to table in MySQL?

WBOY
WBOYforward
2023-09-23 17:29:021234browse

如何在 MySQL 中将逗号分隔的列表设置为表?

You can use UNION ALL to achieve this.

Let us use UNION ALL to make the list 10, 20, 30, 40, 50 as a table -

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 list 1,2,3 as table, use following 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)

The above is the detailed content of How to set comma separated list to table in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete