How to create a dynamic view in mysql

17 Views Asked by At

I want to create a dynamic_view using a stored procedure for the following scenario.

Table1 has two columns (id, name), and it has the following three records.

id|name
-------
1 | a
2 | b
3 | c

Table2 has four columns (id, date, table1_id, value) that stores values for table1 records as following:

id | date    |table1_id | value
------------------------------ 
1  | 2024-1-1| 1         | 2.4
2  | 2024-1-1| 2         | 2.5
3  | 2024-1-1| 3         | 2.1
4  | 2024-1-2| 2         | 2
5  | 2024-1-2| 3         | 2.3

Now, I want a stored procedure to create a view dynamically that has four columns (id, date, a, b, c) based on table1 records for columns and their records should be based on table2 data. this id should be auto-incremental. like the following

id | date    |a   | b   | c
----------------------------
1  | 2024-1-1|2.4 | 2.5 |2.1
2  | 2024-1-2|null| 2   |2.3 

Your support is highly appreciate it.

0

There are 0 best solutions below