Mysql从相似但不相关的表中获取数据

Mysql从相似但不相关的表中获取数据,mysql,stored-procedures,union,fetch,Mysql,Stored Procedures,Union,Fetch,我的数据库中有如下产品表: shop1_products shop2_products shop3_products ..... .... ... shop100_products 我想展示使用union尝试过的所有表中的产品,但我认为使用100个表并不合适 我知道,如果我们制作一个产品表,其中包含shopid列,将使它变得简单。但是在我的网站上 逻辑是不同的,我不能这样做,因为这种结构会迫使我在其他模块上做双重工作 我想用存储过程来实现这一点,但我对存储过程是新手 你认为,我们怎样才能以有效

我的数据库中有如下产品表:

shop1_products
shop2_products
shop3_products
.....
....
...
shop100_products
我想展示使用
union
尝试过的所有表中的产品,但我认为使用100个表并不合适

我知道,如果我们制作一个产品表,其中包含shopid列,将使它变得简单。但是在我的网站上 逻辑是不同的,我不能这样做,因为这种结构会迫使我在其他模块上做双重工作

我想用存储过程来实现这一点,但我对
存储过程是新手

你认为,我们怎样才能以有效的方式做到这一点

如果
存储过程
最适合这样做,请提供示例代码或参考链接

do like this put all the table names in a temp table then follow these steps in while loop.
Then create a table results with all the required columns

for each @tablename
insert into results
select product name ,id,category... from @tablename
repeat

Then finally select distinct * from results
---------------------------------代码-----------------------------------------------------

create table temp(id int auto_increment primary key,tblname varchar(100));

insert into temp(tblname)
VALUES('shop1_products'),('shop2_products'),('shop3_products')...('shop100_products');


select min(id),max(id) into @vmin,@vmax from temp;
select @vmin,@vmax;

create table results(productname varchar(100),id int,category varchar(100)...);

while(@vmin <= @vmax)
Do
select tblname into @tablename from temp;

INSERT INTO results(product name ,id,category...)
select product name ,id,category... from @tablename

SET @vmin=@vmin+1;

END WHILE;

select distinct  * from results;
create table temp(id int auto_increment主键,tblname varchar(100));
插入临时文件(tblname)
价值观(“shop1_产品”)、(“shop2_产品”)、(“shop3_产品”)、(“shop100_产品”);
在@vmin中选择最小(id)、最大(id),从temp中选择@vmax;
选择@vmin、@vmax;
创建表格结果(productname varchar(100)、id int、类别varchar(100)…);

虽然(@vmin
UNION
为什么不起作用?表的架构有什么不同?架构是一样的,但当没有表增长到100..200…UNION会变慢你需要从所有表中获得多少列?它只是productname还是其他什么?@AnandPhadke:是的产品名称、id、类别…最多5-7列嗯,是的。这很慢,因为你试图从100个表中选择并实现一个组合结果…无论你做什么都会受到影响,因为这是你的问题的本质。如果你想在不从100个表中选择的情况下获取所有产品,你将需要重新构造数据库。这不是我刚才给你的原型语法关于@AnandPhadke的过程:你能给我提供准确的语法吗?正如我说的,我是存储过程的新手。非常感谢。我会根据需要对它进行更改,如果需要进一步的帮助,我会发表评论