用mysql创建函数

nszi6y05  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(237)

你好,我有一个任务:考虑一个有两个关系的雇员数据库:

employee(employee-name, street, city)
works(employee-name, company-name, salary)

其中主键带下划线。写一个查询,找出那些员工平均工资低于“第一银行公司”平均工资的公司。根据需要使用自定义sql函数(create function命令)回答上述查询,函数以公司名称作为输入,返回给定公司的平均工资。我创建了这个函数:

create function avg_salary (c_name varchar(30))
    returns numeric(8,6)
begin
        declare a_salary numeric(8,6);

        select avg(salary) into  a_salary
        from works
        where company.company_name = c_name
        group by company_name; 

        return a_salary; 

        select company_name
        from works
        where a_salary<(select avg(salary) from works where company_name = ’ First Bank Corporation’);
end

但我收到一条错误信息:
错误1415(0a000):不允许从函数返回结果集
我不明白为什么我会犯这个错误。谢谢你的帮助

2vuwiymt

2vuwiymt1#

试试这个(示例包括我用来创建所有对象和插入数据的所有代码):

DROP TABLE IF EXISTS employee;
CREATE TABLE IF NOT EXISTS employee (
    `employee-name` varchar(100) NOT NULL,
    `street` varchar(100) NOT NULL,
    `city` varchar(100) NOT NULL,
    PRIMARY KEY (`employee-name`)
    );

DROP TABLE IF EXISTS works;
CREATE TABLE IF NOT EXISTS works (
    `employee-name` varchar(100) NOT NULL,
    `company-name` varchar(100) NOT NULL,
    `salary` numeric(8,2) NOT NULL,
    PRIMARY KEY (`employee-name`)
    );

INSERT INTO 
    employee 
        (`employee-name`, `street`, `city`)
    VALUES
        ('Employee 01', '1234 State ST.', 'Your City'),
        ('Employee 02', '1234 State ST.', 'Your City'),
        ('Employee 03', '1234 State ST.', 'Your City'),
        ('Employee 04', '1234 State ST.', 'Your City'),
        ('Employee 05', '1234 State ST.', 'Your City'),
        ('Employee 06', '1234 State ST.', 'Your City'),
        ('Employee 07', '1234 State ST.', 'Your City'),
        ('Employee 08', '1234 State ST.', 'Your City'),
        ('Employee 09', '1234 State ST.', 'Your City'),
        ('Employee 10', '1234 State ST.', 'Your City'),
        ('Employee 11', '1234 State ST.', 'Your City'),
        ('Employee 12', '1234 State ST.', 'Your City'),
        ('Employee 13', '1234 State ST.', 'Your City'),
        ('Employee 14', '1234 State ST.', 'Your City'),
        ('Employee 15', '1234 State ST.', 'Your City'),
        ('Employee 16', '1234 State ST.', 'Your City'),
        ('Employee 17', '1234 State ST.', 'Your City'),
        ('Employee 18', '1234 State ST.', 'Your City'),
        ('Employee 19', '1234 State ST.', 'Your City'),
        ('Employee 20', '1234 State ST.', 'Your City'),
        ('Employee 21', '1234 State ST.', 'Your City'),
        ('Employee 22', '1234 State ST.', 'Your City');

INSERT INTO 
    works 
        (`employee-name`, `company-name`, `salary`)
    VALUES
        ('Employee 01', 'Company 01', 10000.00),
        ('Employee 02', 'Company 01', 10000.00),
        ('Employee 03', 'Company 01', 10000.00),
        ('Employee 04', 'Company 01', 10000.00),
        ('Employee 05', 'Company 01', 10000.00),
        ('Employee 06', 'Company 02', 15000.00),
        ('Employee 07', 'Company 02', 15000.00),
        ('Employee 08', 'Company 02', 15000.00),
        ('Employee 09', 'Company 03', 20000.00),
        ('Employee 10', 'Company 03', 20000.00),
        ('Employee 11', 'Company 03', 20000.00),
        ('Employee 12', 'Company 03', 20000.00),
        ('Employee 13', 'Company 03', 20000.00),
        ('Employee 14', 'Company 03', 20000.00),
        ('Employee 15', 'Company 03', 20000.00),
        ('Employee 16', 'Company 04', 60000.00),
        ('Employee 17', 'Company 04', 60000.00),
        ('Employee 18', 'Company 04', 60000.00),
        ('Employee 19', 'First Bank Corporation', 17000.00),
        ('Employee 20', 'First Bank Corporation', 17000.00),
        ('Employee 21', 'First Bank Corporation', 17000.00),
        ('Employee 22', 'First Bank Corporation', 17000.00);

DROP FUNCTION IF EXISTS avg_salary;
DELIMITER $$
CREATE FUNCTION IF NOT EXISTS avg_salary (c_name varchar(100)) 
    RETURNS numeric(8,2)   
    BEGIN
        DECLARE a_salary numeric(8,2);
        SELECT avg(salary) INTO a_salary FROM works WHERE `company-name` = c_name GROUP BY `company-name`;
        RETURN a_salary;
    END$$
DELIMITER ;

SELECT
    `company-name`
FROM 
    works
WHERE
    avg_salary(`company-name`) < avg_salary('First Bank Corporation')
GROUP BY
    `company-name`

它非常适合我,返回公司01和公司02作为结果。
原版海报评论后添加。查询现在返回平均工资和公司名称,

SELECT
    `company-name` AS `Company Name`,
    avg_salary(`company-name`) AS `AVG Salary`
FROM 
    works
WHERE
    avg_salary(`company-name`) < avg_salary('First Bank Corporation')
GROUP BY
    `company-name`
tf7tbtn2

tf7tbtn22#

您应该结合教程阅读mysql手册来理解mysql的细节。
错误信息非常清楚(每个select返回一个结果集),因此select company\u name from works是错误的。选择into和set=(select…)可以。除了这个错误外,“first bank corporation”周围的单引号在我试图编译函数时也是奇数和错误。也可按公司名称分组;将在以后版本的my sql中被拒绝,因为选择列表中不包括公司名称。看到了吗https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html -在这种情况下,它是不需要的,因为你是一个特定的公司avgerageing。mysql还要求在create函数之前和之后设置分隔符,其中函数中有多个语句请参见https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html

oxosxuxt

oxosxuxt3#

尝试以下操作:

create function avg_salary (c_name varchar(30))
        returns numeric(18,6)
         begin
        declare a_salary numeric(18,6);
        set a_salary = select avg(salary) from works where company_name = c_name; 
        return (a_salary); 
    end

select company_name from works where a_salary<(select avg_salary('First Bank Corporation'));

相关问题