HANDLING FIXED VALUE ARRAY AS TABLE

Asked 1 years ago, Updated 1 years ago, 33 views

For example
f(x)=x*x(x=1..10)
When I want to make a table of , how can I make a part of 1...10?

CREATE TEMPORARY TABLE x(
  x bigint(20)
);

INSERT x VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);

SELECT x, x* x FROM x

I wonder if I have no choice but to make a table like this

SELECT x, x* x FROM 
VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)

Can't you write something like this?

Supplemental:
This is MySQL 5.7

mysql

2022-09-30 14:58

1 Answers

With MySQL 8.0, you can write like this.

 WITH RECURSIVE AS(
  SELECT 1 AS x, 1 AS xx UNION ALL SELECT x +1, (x +1)*(x +1) FROM t WHERE x <10
)
SELECT* FROM t;
+------+------+
| x|xx|
+------+------+
|    1 |    1 |
|    2 |    4 |
|    3 |    9 |
|    4 |   16 |
|    5 |   25 |
|    6 |   36 |
|    7 |   49 |
|    8 |   64 |
|    9 |   81 |
|   10 |  100 |
+------+------+


2022-09-30 14:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.