【发布时间】:2016-10-21 08:08:15
【问题描述】:
我需要知道是否可以使用 MySQL 中的存储过程从 temptable 中读取日期并再次将该数据插入另一个 temptable
我想从一个临时表中读取数据,然后按照我希望插入的任何顺序再次将该数据插入到另一个临时表中,这可能吗?
temptable1(读取) --------> temptable2(写入)
提前致谢
这是我的存储过程
DELIMITER //
create procedure search(
in search_key varchar(255)
)
BEGIN
-- Drop the temp table if already exist
DROP temporary table IF EXISTS temptable;
-- Creating temp table
create temporary table temptable(
SELECT member .member_type
FROM member member
LEFT JOIN org_person orgPerson
ON orgPerson.person_id = member .id
INNER JOIN organization organization
ON organization.id = orgPerson.org_id
ORDER BY organization.name);
-- Drop the temp table if already exist
DROP temporary table IF EXISTS temptableSearch;
-- Creating another temp table
create temporary table temptableSearch like temptable;
-- Inserting value from table
insert into temptableSearch
select *
from temptable temp
where temp.name like 'search_key'
UNION
select *
from temptable temp
where temp.name like 'search_key%'
UNION
select *
from temptable temp
where temp.name like '%search_key%';
select DISTINCT *
from temptableSearch;
END //
DELIMITER ;
当我调用它时,它会显示以下错误
Error Code: 1137. Can't reopen table: 'temp'
我发现只有这部分会发生错误
insert into temptableAffiliationSearch
select *
from temptableAffiliation temp
where temp.name like 'search_key'
UNION
select *
from temptableAffiliation temp
where temp.name like 'search_key%'
UNION
select *
from temptableAffiliation temp
where temp.name like '%search_key%';
【问题讨论】:
-
为什么不可能呢?你应该尝试一下你脑海中出现的第一个想法,它可能会奏效。 (如果没有,您应该在此处发布该代码)。只是“按我希望的任何顺序”这一点并不完全准确,因为表中的行没有顺序。当您阅读它们时,他们会收到订单(例如
select * from table order by somecolumn)。但是您可以在新表中使用 auto_increment 列并按此排序。 -
它再次显示同样的错误
-
如果你不介意的话,你能重写我的代码吗
-
为什么只要用正确的 where 条件查询同一个表 3 次?
标签: mysql sql database stored-procedures