学习存储程序,简单查询(在出去……)

iugsix8n  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(263)

我正在努力做到以下几点:


# 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;

这是我为明天的考试做的练习,我正在尝试创建一个过程,所以当我把作者的名字放在电话上时,它会从他/她那里取出所有的书。
table:
作者:authornum,authorlast,authorfirst
书籍:书号,书名,出版代号,类型,平装本
写:书号,作者,顺序
我为您创建了一个元组关系计算,如果有帮助的话:

//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';

请帮忙
谢谢您!

bnl4lu3b

bnl4lu3b1#

好吧,所以我发现了问题,老实说,这不符合我自己练习的目的,因为我想练习进进出出。。。但我想我学到了一些东西,尽管时机不对。问题是mysql假设into只返回1行,而我在那里发送的不止1行,所以在本例中有两个选项:
如何从mysql存储过程中检索多行?
方案一:再填一张表,可能是临时表。
方案b:只执行select语句,而不执行过程中的into子句;然后可以从应用程序读取数据集(c#,php+mysqli,…)
方案c:不使用过程,只执行select查询。
来自stackoverflow的源
这是我最后的密码:


# 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');
xqkwcwgp

xqkwcwgp2#

对我来说很好-检查您的call语句(您似乎发现过程名称和call语句之间不匹配)和数据

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)

相关问题