学习存储过程,简单查询(IN ... OUT ...)

Learning store procedures, simple query (IN ... OUT...)

我正在尝试执行以下操作:

    # get_all_books_by_author

    drop procedure if exists func;

    delimiter ^^
    create procedure func(in authName varchar(20), out books varchar(255))
    begin
        select book.title
        into books
        from book
        INNER JOIN wrote on wrote.bookcode = book.bookcode
        INNER JOIN author on author.authornum = wrote.authornum    
        where authName = author.authorFirst;

    end ^^
    delimiter ;

call func('Toni', @books); 
select @books;

这是我明天的考试练习,我正在尝试创建程序,所以当我在电话中输入作者姓名时,它会从 him/her.[=14 中获取所有书籍=]

表格:

author: authorNum, authorLast, authorFirst

book: bookCode, title, publisherCode, type, paperback

wrote: bookCode, authorNum, sequence

如果对您有帮助,我为您创建了一个元组关系计算:

//The E means the existential quantifier 

{ a.title | book(a) AND (Eb)( wrote(b) AND (Ec)( Author(c) AND
b.authornum=c.authornum AND b.bookcode=a.bookcode AND
c.authorFirst='name_of_the_OUT' ))}

如果上面的表达有误,请告诉我,我也在学习它,哈哈,但我认为它是正确的。我感兴趣的是 sql 使用过程查询本身。

我尝试了这个基本的 SQL 并且可以工作,但是我无法让存储过程工作:

select title
from book
INNER JOIN wrote on wrote.bookcode = book.bookcode
INNER JOIN author on author.authornum = wrote.authornum
WHERE author.authorFirst = 'Toni';

请帮忙

谢谢!

对我来说工作正常 - 检查你的调用语句(你似乎在过程名称和调用语句之间不匹配)和数据

drop table if exists book,author,wrote;
create table book (bookcode int,title varchar(10));
create table author(authornum int, authorfirst varchar(5));
create table wrote(bookcode int, authornum int);

insert into book values(1,'aaa'),(2,'bbb');
insert into wrote values(1,1),(2,2);
insert into author values(1,'toni'),(2,'cyril');

drop procedure if exists func;

    delimiter ^^
    create procedure func(in authName varchar(20), out books varchar(255))
    begin
        select book.title
        into books
        from book
        INNER JOIN wrote on wrote.bookcode = book.bookcode
        INNER JOIN author on author.authornum = wrote.authornum    
        where authName = author.authorFirst;

    end ^^
    delimiter ;

call func('Toni', @books); 
select @books;

+--------+
| @books |
+--------+
| aaa    |
+--------+
1 row in set (0.00 sec)

好的,所以我发现了问题,老实说,这并没有达到我自己练习的目的,因为我想练习 IN,OUT ...但我想我学到了一些东西,即使它不在适当的时候。 问题是 MYSQL 假定 INTO 将 return 只有 1 行,而我在那里发送不止 1 行,所以在这种情况下有几个选项:

如何从 mysql 中的存储过程中检索多行?

Plan A: Fill another table, it may be a temporary table.

Plan B: Just execute your SELECT statement without INTO clause from the procedure; then you could read data-set from the application (c#, PHP+mysqli,...)

Plan C: Do not use the procedure, just execute the SELECT query.

Source from Whosebug

这是我的最终代码:

# get_all_books_by_author

drop procedure if exists get_all_books_by_author;

delimiter ^^
create procedure get_all_books_by_author(in authName varchar(20))
begin
    select book.title
    from book
    INNER JOIN wrote on wrote.bookcode = book.bookcode
    INNER JOIN author on author.authornum = wrote.authornum  
    WHERE author.authorFirst = authName;


end ^^
delimiter ;

call get_all_books_by_author('Toni');