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

How to set comma separated list to table in MySQL?

WBOY
Release: 2023-09-23 17:29:02
forward
1249 people have browsed it

如何在 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;
Copy after login

Output

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

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;
Copy after login

output

+---+
| a |
+---+
| 1 |
| 2 |
| 3 |
+---+
3 rows in set (0.00 sec)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template