创建一个接受结果集作为参数的mysql函数?

创建一个接受结果集作为参数的mysql函数?,mysql,Mysql,是否可以创建一个mySQL函数,将查询结果集作为参数接受 基本上,我有很多查询将返回如下结果集: id | score 70 | 25 71 | 7 72 | 215 74 | 32 75 | 710 76 | 34 78 | 998 79 | 103 80 | 3 但是,我无法提出一个查询来获取这个查询中的最小值和最大值以及结果,因此我考虑使用一个函数(不能使用存储过程),但无法提供有

是否可以创建一个mySQL函数,将查询结果集作为参数接受

基本上,我有很多查询将返回如下结果集:

id | score 70 | 25 71 | 7 72 | 215 74 | 32 75 | 710 76 | 34 78 | 998 79 | 103 80 | 3 但是,我无法提出一个查询来获取这个查询中的最小值和最大值以及结果,因此我考虑使用一个函数(不能使用存储过程),但无法提供有关如何传递结果集的文档

感谢您的帮助
谢谢

编辑: 结果中的分数字段是一个计算字段。不能直接选择它

例如:返回上述结果的示例查询-

选择t.id作为id,计数(*)作为分数
来自tbl t
t.idx=t2.idx上的内部连接tbl2 t2
其中t2.在(…)中的角色

仅用于演示目的,而不是实际的模式或查询

否。MySQL不支持使用resultset作为参数定义函数

不幸的是,MySQL不支持公共表表达式(CTE),也不支持分析函数

要从MySQL查询中获取此结果。。。在MySQL中实现这一点的一种方法是将原始查询作为内联视图返回两次

例如:

SELECT t.id
     , (t.score-s.min_score)/(s.max_score-s.min_score) AS normalized_score
  FROM ( 
         -- original query here
         SELECT id, score FROM ...
       ) t
 CROSS
  JOIN ( SELECT MIN(r.score) AS min_score
              , MAX(r.score) AS max_score
           FROM (
                  -- original query here
                  SELECT id, score FROM ...   
                ) r
       ) s
 ORDER BY t.id 
编辑

根据添加到问题中的查询

SELECT q.id
     , (q.score-s.min_score)/(s.max_score-s.min_score) AS normalized_score
  FROM ( -- original query goes here
         -- ------------------------ 

                  select t.id as id, count(*) as score 
                  from tbl t 
                  inner join tbl2 t2 on t.idx = t2.idx
                  where t2.role in (.....)

         -- ------------------------ 
       ) q
 CROSS
  JOIN ( SELECT MIN(r.score) AS min_score
              , MAX(r.score) AS max_score
           FROM ( -- original query goes here
                  -- ------------------------ 

                  select t.id as id, count(*) as score 
                  from tbl t 
                  inner join tbl2 t2 on t.idx = t2.idx
                  where t2.role in (.....)

                  -- ------------------------ 
                ) r
       ) s
 ORDER BY q.id 

我很抱歉。忘了提一个重要的细节。结果中的分数字段是计算字段。不能直接选择它。编辑问题以反映这一点。“计算字段”不应成为问题。我们只需要一个返回有效结果集的查询。这个完整的查询进入了parens。不幸的是,同一个查询必须指定两次。是的,长度是个问题,特别是当存在多个连接时。但我猜这得凑合了。谢谢@spencer7593
SELECT q.id
     , (q.score-s.min_score)/(s.max_score-s.min_score) AS normalized_score
  FROM ( -- original query goes here
         -- ------------------------ 

                  select t.id as id, count(*) as score 
                  from tbl t 
                  inner join tbl2 t2 on t.idx = t2.idx
                  where t2.role in (.....)

         -- ------------------------ 
       ) q
 CROSS
  JOIN ( SELECT MIN(r.score) AS min_score
              , MAX(r.score) AS max_score
           FROM ( -- original query goes here
                  -- ------------------------ 

                  select t.id as id, count(*) as score 
                  from tbl t 
                  inner join tbl2 t2 on t.idx = t2.idx
                  where t2.role in (.....)

                  -- ------------------------ 
                ) r
       ) s
 ORDER BY q.id