简体   繁体   English

MySQL在select语句中使用存储过程

[英]MySQL Using stored procedure in select statement

I have a stored procedure like so: 我有一个这样的存储过程:

$connection->query('
   drop procedure if exists listing_count;
   create procedure listing_count(IN parent int(11))
   begin
    declare count1 int(11) default 0;
    declare count2 int(11) default 1;
    create temporary table ids as (select id from category where id=parent);
    while count1<>count2 do
     set count1=(select count(id) from ids);
     insert into ids(id) select id from category where id not in(select id from ids) and related in(select id from ids);     
     set count2=(select count(id) from ids);
    end while;
    (select count(*) from listing_category where category in(select id from ids));
   end');

$fetch=$connection->query('select *,listing_count(id) as listing_count from category')->fetchall(pdo::FETCH_UNIQUE|pdo::FETCH_ASSOC);

I would like to use my procedure like a function. 我想像函数一样使用我的程序。 So that listing_count gets the count so that I can use it. 这样, listing_count就可以得到计数,以便我可以使用它。 Do I need to create a separate function? 我需要创建一个单独的功能吗? Can a procedure get my count and return it? 程序可以获取我的计数并返回吗?

Turning it into a function like so: 将其转换为如下功能:

drop function if exists listing_count;
   create function listing_count(parent int(11)) returns int(11) deterministic
   begin
    declare count1 int(11) default 0;
    declare count2 int(11) default 1;
    create temporary table ids as (select id from category where id=parent);
    while count1<>count2 do
     set count1=(select count(id) from ids);
     insert into ids(id) select id from category where id not in(select id from ids) and related in(select id from ids);     
     set count2=(select count(id) from ids);
    end while;
    return (select count(*) from listing_category where category in(select id from ids));
   end

But this does not work. 但这是行不通的。 I am not very familiar with procedures vs functions but I assume that I can't add all the functionality into a function as I can in a procedure. 我对过程与函数不是很熟悉,但是我认为我无法像在过程中那样将所有功能添加到函数中。

I would like to use my procedure like a function. 我想像函数一样使用我的程序。

You Can't Do That™. 您不能那样做™。

I suggest you convert your sp to a stored function. 我建议您将sp转换为存储函数。 That's a good idea in any case because it returns a single value. 在任何情况下,这都是一个好主意,因为它返回一个值。 The way you have it now, it returns a one-column one-row result set. 您现在所拥有的方式,它将返回一个单列单行结果集。 If it were a function it would work easily in every context you need it. 如果它是一项功能,它将很容易在您需要的每种情况下运行。 In contrast, stored procedures returning result sets are not nearly as easy to use. 相比之下,返回结果集的存储过程几乎不那么容易使用。 For example, see this. 例如,参见此。 How to use Table output from stored MYSQL Procedure 如何使用存储的MYSQL过程中的表输出

Or you could write a stored function to wrap your stored procedure and return the value. 或者,您可以编写一个存储函数来包装存储过程并返回值。 In my opinion that is an inferior solution, just because it has extra complexity. 我认为这是次等的解决方案,因为它具有额外的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM