mysql - mysql使用phpmyadmin在存储过程中调用函数
问题描述
我有以下有效的代码:
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE user_id int(11);
DECLARE cur1 CURSOR FOR SELECT id FROM users;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
# drop and re-create user_rank TABLE
DROP TABLE IF EXISTS user_rank;
CREATE TABLE `user_rank` (
`id` int(11) UNSIGNED NOT NULL,
`has_hobbies` int(3) DEFAULT 0,
`passed_test` int(3) DEFAULT 0,
`has_picture` int(3) DEFAULT 0,
`won_a_job` int(3) DEFAULT 0,
`is_prolancer` int(3) DEFAULT 0,
`is_verified` int(3) DEFAULT 0,
`has_portfolio` int(3) DEFAULT 0,
`has_likes` int(3) DEFAULT 0,
`has_disputes` int(3) DEFAULT 0,
`has-earnings` int(3) DEFAULT 0,
`has_feebacks` int(3) DEFAULT 0,
`has_invitations` int(3) DEFAULT 0,
`has_views` int(3) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `user_rank`
ADD PRIMARY KEY (`id`);
ALTER TABLE `user_rank`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO user_id;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO user_rank (id) values (user_id);
END LOOP;
CLOSE cur1;
END
它使用游标在用户表内循环,并将所有用户 ID 复制到表 user_rank 中。我在我的数据库中定义了一个名为“hasUserPassedTest”的函数,给定用户 ID 返回 10 或 0。我想在上面的循环中调用该函数并将其插入 user_rank 表中,但以下代码不起作用:
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE user_id int(11);
DECLARE cur1 CURSOR FOR SELECT id FROM users;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
# drop and re-create user_rank TABLE
DROP TABLE IF EXISTS user_rank;
CREATE TABLE `user_rank` (
`id` int(11) UNSIGNED NOT NULL,
`has_hobbies` int(3) DEFAULT 0,
`passed_test` int(3) DEFAULT 0,
`has_picture` int(3) DEFAULT 0,
`won_a_job` int(3) DEFAULT 0,
`is_prolancer` int(3) DEFAULT 0,
`is_verified` int(3) DEFAULT 0,
`has_portfolio` int(3) DEFAULT 0,
`has_likes` int(3) DEFAULT 0,
`has_disputes` int(3) DEFAULT 0,
`has-earnings` int(3) DEFAULT 0,
`has_feebacks` int(3) DEFAULT 0,
`has_invitations` int(3) DEFAULT 0,
`has_views` int(3) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `user_rank`
ADD PRIMARY KEY (`id`);
ALTER TABLE `user_rank`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO user_id;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO user_rank (id, has_hobbies) values (user_id, CALL
hasUserPassedTest(user_id));
END LOOP;
CLOSE cur1;
END
我正在使用 CALL 在存储过程中调用我的函数,但不起作用。如何在存储过程中调用我的函数?
解决方案
我正在使用 CALL 在存储过程中调用我的函数但不起作用
CALL
不用于执行功能。
CALL 语句调用先前使用 CREATE PROCEDURE 定义的存储过程。
它使用游标在用户表内循环,并将所有用户 ID 复制到表 user_rank 中。
为什么这么复杂?过程、游标、处理程序、循环......一个简单的查询就足够了:
INSERT INTO user_rank (id, has_hobbies)
SELECT user_id, hasUserPassedTest(user_id)
FROM users;
更新
插入可以与表创建相结合:
CREATE TABLE `user_rank` (
`id` int(11) UNSIGNED NOT NULL,
`has_hobbies` int(3) DEFAULT 0,
`passed_test` int(3) DEFAULT 0,
`has_picture` int(3) DEFAULT 0,
`won_a_job` int(3) DEFAULT 0,
`is_prolancer` int(3) DEFAULT 0,
`is_verified` int(3) DEFAULT 0,
`has_portfolio` int(3) DEFAULT 0,
`has_likes` int(3) DEFAULT 0,
`has_disputes` int(3) DEFAULT 0,
`has-earnings` int(3) DEFAULT 0,
`has_feebacks` int(3) DEFAULT 0,
`has_invitations` int(3) DEFAULT 0,
`has_views` int(3) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SELECT user_id AS id, hasUserPassedTest(user_id) AS has_hobbies
FROM users;
一个微妙之处 - 插入的字段将是表结构中的最后一个。如果字段顺序有意义,则必须在选择部分提及所有字段:
CREATE TABLE `user_rank` (
`id` int(11) UNSIGNED NOT NULL,
`has_hobbies` int(3) DEFAULT 0,
`passed_test` int(3) DEFAULT 0,
`has_picture` int(3) DEFAULT 0,
`won_a_job` int(3) DEFAULT 0,
`is_prolancer` int(3) DEFAULT 0,
`is_verified` int(3) DEFAULT 0,
`has_portfolio` int(3) DEFAULT 0,
`has_likes` int(3) DEFAULT 0,
`has_disputes` int(3) DEFAULT 0,
`has-earnings` int(3) DEFAULT 0,
`has_feebacks` int(3) DEFAULT 0,
`has_invitations` int(3) DEFAULT 0,
`has_views` int(3) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8
SELECT user_id AS id,
hasUserPassedTest(user_id) AS has_hobbies
0 AS `passed_test`,
0 AS `has_picture`,
0 AS `won_a_job`,
0 AS `is_prolancer`,
0 AS `is_verified`,
0 AS `has_portfolio`,
0 AS `has_likes`,
0 AS `has_disputes`,
0 AS `has-earnings`,
0 AS `has_feebacks`,
0 AS `has_invitations`,
0 AS `has_views`
FROM users;
DROP TABLE
保持一个单独的查询:(
推荐阅读
- linux - 有没有办法设置 linux 在程序崩溃时显示 GUI 对话框?
- python - python中的多线程-我想将字典作为参数传递
- c++ - SendInput() 与 GetAsyncKeyState() (WinAPI) 一起使用时会导致无限循环
- node.js - 如何自动重启nodejs cli脚本
- php - Eloquent 获取关系表点数最高的用户
- reactjs - 为什么'emailjs'没有被定义为no-undef?
- windows-10 - 如何使用注册表更改任务栏位置?
- javascript - 如何用返回媒体 url 的函数替换 HTMLMediaElement.prototype.play
- reactjs - 防止 msal-react 多次加载组件
- windows - 如何在下拉列表CComboBox中设置项目的高度