0

以下のようなデータベースに製品テーブルがあります。

shop1_products
shop2_products
shop3_products
.....
....
...
shop100_products

試したサイトのすべてのテーブルの製品を表示したいのですがunion、100 テーブルではうまくいかないと思います。

shopid列を含む1つの製品テーブルを作成することでそれを行うと、それが簡単になることを私は知っています. しかし、私のウェブサイトのロジックは異なります。この構造では、他のモジュールで二重の作業を行う必要があるため、それはできません。

私はストアドプロシージャでこれを行うことを考えていますが、stored procedure.

効率的な方法でこれを行うにはどうすればよいと思いますか?

これを行うのが最善の場合stored procedureは、サンプルコードまたは参照リンクを提供してください

4

1 に答える 1

1
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;
于 2012-08-31T10:47:49.260 に答える