0

クレイジーな関数を作成するには、助けが必要です。

と呼ばれるテーブルとテーブルを取得しましproductcountryCountriesIdこの製品テーブルには、のようなIDで呼び出される列があります1,2,3,4

製品テーブルを選択するときに、CountriesId列で返されたIDをテーブルの説明に置き換えることができるストアド関数を作成する必要がありますCountry

例:

SELECT * FROM Country;

結果:

CountryId | CountryDescription
1         | U.S.A
2         | Japan
3         | China

SELECT * FROM Product;

結果:

ProductId | ProductDescription | CountriesId
1         | Product 1          | 1,2,3

SELECT fnIdToDescription(CountriesId) AS Countries FROM Product;

期待される結果:米国、日本、中国の国

簡単に作れるかもしれませんが、解決策が見つかりませんでした。

ありがとう!

4

2 に答える 2

1

You will be doing a lot better now and in the future if you change your DB design now. It is violating the first normalisation rule.

Remove the countryIds column from your Product table and create a new table CountryProduct that stores the relations between the tables. Example:

CREATE TABLE CountryProduct
(
   countryId INT NOT NULL,      `
   productId INT NOT NULL,
   PRIMARY KEY (countryId, productId) 
);
于 2012-11-27T14:04:24.103 に答える
0

GROUP_CONCAT()という関数を見つけ、説明列にGROUP_CONCAT()を指定してIDの副選択を使用して解決しました。

皆さんありがとう。

于 2012-11-29T16:44:54.987 に答える