Using a stored procedure as subquery

后端 未结 1 1868
深忆病人
深忆病人 2021-01-24 19:42

I\'ve create a stored procedure in MySQL like the this

CREATE DEFINER=`root`@`localhost` PROCEDURE `my_proc`(IN var1 VARCHAR(25))
BEGIN
select (sum(er)*9)/(out/3         


        
相关标签:
1条回答
  • 2021-01-24 20:21

    You can define a stored function instead of a stored procedure.

    CREATE DEFINER=`root`@`localhost` FUNCTION `my_func`(IN var1 VARCHAR(25))
      RETURNS NUMERIC(9,2)
    BEGIN
      RETURN (SELECT (SUM(er)*9)/(out/3) AS era FROM table1 WHERE id = var1);
    END
    

    Then you can call it simply as you would call a function:

    SELECT id, column1, column2, my_func(table1.id) AS era FROM table1
    

    The stored function must be guaranteed to return a single scalar to be usable in your select-list.

    I removed the GROUP BY, since it's superfluous.

    The example above is kind of suspicious, because there's no reason to call a function like this to calculate the SUM over a single row. But I guess you have something more complex in mind.

    0 讨论(0)
提交回复
热议问题