How to work with array variable in MySQL?

MySQLMySQLi Database

MySQL does not support array variables. To get the same result, use the table DUAL. Following is the syntax:

SELECT yourValue1 AS ArrayValue FROM DUAL
UNION ALL
SELECT yourValue2 FROM DUAL
UNION ALL
SELECT yourValue3 FROM DUAL
UNION ALL
SELECT yourValue4 FROM DUAL
UNION ALL
.
.
.
.
.
.
SELECT yourValueN FROM DUAL;

Let us create a sample table:

mysql> SELECT 1 AS ArrayValue FROM DUAL
      UNION ALL
      SELECT 2 FROM DUAL
      UNION ALL
      SELECT 3 FROM DUAL
      UNION ALL
      SELECT 4 FROM DUAL
      UNION ALL
      SELECT 5 FROM DUAL
      UNION ALL
      SELECT 6 FROM DUAL
      UNION ALL
      SELECT 7 FROM DUAL
      UNION ALL
      SELECT 8 FROM DUAL
      UNION ALL
      SELECT 9 FROM DUAL
      UNION ALL
      SELECT 10 FROM DUAL ;

This will produce the following output

+------------+
| ArrayValue |
+------------+
| 1          |
| 2          |
| 3          |
| 4          |
| 5          |
| 6          |
| 7          |
| 8          |
| 9          |
| 10         |
+------------+
10 rows in set (0.00 sec)
raja
Published on 17-Apr-2019 15:01:50
Advertisements