MySQL 在 select 语句中生成带有循环的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8861734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL producing columns with loop in a select statement
提问by Trees4theForest
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.
谢谢。
采纳答案by DRapp
yes you can... take a look into DynamicSQL..
是的,你可以......看看DynamicSQL..
In general, you build a string that contains the SQL statement you want to execute, then prepare it, then execute it...
通常,您构建一个包含要执行的 SQL 语句的字符串,然后准备它,然后执行它...