我正试图围绕 mySQL 中的函数展开思考,我目前正在制作一个检查列account_description
的函数,它的值是查看描述是否已经存在。
如果它已经存在,则显示一条消息。但是,如果描述不存在,则显示一条不同的消息,说明未找到。
谢谢!
MySQL 代码:
DROP FUNCTION IF EXISTS test_glaccounts_description
DELIMITER //
CREATE FUNCTION test_glaccounts_description
(
check_description VARCHAR(50)
)
RETURNS VARCHAR(50)
BEGIN
DECLARE var_check VARCHAR(50);
SELECT
account_description INTO var_check
FROM
general_ledger_accounts
WHERE
account_description = check_description;
IF var_check = check_description THEN
SELECT 'That description already exists.';
ELSEIF var_check != check_description THEN
SELECT 'That description does not exist.';
END IF;
RETURN var_check;
END //
DELIMITER ;
SELECT
test_glaccounts_description(account_description) as 'Check'
FROM
general_ledger_accounts
WHERE
account_description = 'Accounting';