1

I have a table called 'Resources' that looks like this:

   Country        City       Street      Headcount
      UK         Halifax   High Street      20
United Kingdom   Oxford    High Street      30
    Canada       Halifax    North St        40

Because of the nature of the location fields, I need to map them to a single 'Address' field, and so I also have the following table called 'Addresses':

   Country        City       Street                 Address
      UK         Halifax   High Street     High Street, Halifax, UK
    Canada       Halifax    North St     North Street, Halifax, Canada
United Kingdom   Oxford    High Street      High Street, Oxford, UK

(In reality the Address field does add information rather than just combining what is already there.)

I am currently using the following SQL to produce the query:

SELECT Resources.Country, Resources.City, Resources.Street, Addresses.Address,
       Resources.Headcount
FROM   Resources
INNER JOIN Addresses ON Resources.Country = Addresses.Country
       AND Resources.City = Addresses.City
       AND Resources.Street = Addresses.Street

This works for me, but I am worried that I have not seen people use this many ANDs in a single join elsewhere, so don't know if it is a bad idea. (This is simplified version - I may need up to 8 ANDs in a single join in another case) Is this the best way to approach the problem, or is there a better solution?

Thanks

4

1 に答える 1

1

複数の列での結合は問題ありません。これを「恐れる」必要はありません。

「より良い方法」まで。いくつかの変数テーブルを作成し、そこにデータを入れて、その TSQL (DDL と DML) をここに投稿することをお勧めします。次に、いくつかの可能な代替案を取得できます。現在、あなたの質問は漠然としています(質問の「より良い方法はありますか」の部分に関して)

于 2013-02-19T19:12:26.293 に答える