MySQL在select语句中生成带循环的列

时间:2021-10-14 22:25:30

In MySQL I have a function that takes a number argument and spits out a subset of results from another table, based on that number. Implementation currently looks like:

在MySQL中,我有一个函数,它接受一个数字参数,并根据该数字从另一个表中吐出一个结果的子集。目前的实施情况如下:

SELECT 
  id,
  date,
  function(do stuff with value 1) as t1,
  function(do stuff with value 2) as t2,
  function(do stuff with value 3) as t3,
  ...
  function(do stuff with value N) as tN
FROM table

Can you use a loop in a select statement (or even a procedure that builds a table) so the above becomes:

你可以在select语句中使用循环(甚至是构建表的过程),所以上面变成:

SELECT
  id,
  date,
  LOOP x = 1 through N
    function(do stuff with value x) as tx,
  END LOOP
FROM table

Thanks.

谢谢。

1 个解决方案

#1


3  

yes you can... take a look into DynamicSQL..

是的,你可以...看看DynamicSQL ..

Here's one sample

这是一个样本

and another example

另一个例子

In general, you build a string that contains the SQL statement you want to execute, then prepare it, then execute it...

通常,您构建一个包含要执行的SQL语句的字符串,然后准备它,然后执行它...

#1


3  

yes you can... take a look into DynamicSQL..

是的,你可以...看看DynamicSQL ..

Here's one sample

这是一个样本

and another example

另一个例子

In general, you build a string that contains the SQL statement you want to execute, then prepare it, then execute it...

通常,您构建一个包含要执行的SQL语句的字符串,然后准备它,然后执行它...