0

我有一个看起来像这样的查询:

SELECT cu.CustomerCountryID,
       pr.ProviderCountryID,
       po.ProductCountryID
FROM   tblCustomer cu
       INNER JOIN tblProvider pr
         ON cu.ProvID = pr.ProvID
       INNER JOIN tblProduct po
         ON pr.ProdID = po.ProdID 

然后是包含国家名称的第四个表。我想用 CountryName 替换 CountryID。

我现在想不出如何在不产生子查询的情况下做到这一点

谁能建议更快的方法来做到这一点?

谢谢

4

5 回答 5

1

您可以通过多次加入国家/地区表来实现此目的:

SELECT cust_country.CountryName,
   prov_country.CountryName,
   prod_country.CountryName
FROM   tblCustomer cu
   INNER JOIN tblProvider pr
     ON cu.ProvID = pr.ProvID
   INNER JOIN tblProduct po
     ON pr.ProdID = po.ProdID 
   INNER JOIN tblCountry cust_country
     ON cu.CustomerCountryID = cust_country.CountryID
   INNER JOIN tblCountry prov_country
     ON pr.CustomerCountryID = prov_country.CountryID
   INNER JOIN tblCountry prod_country
     ON co.CustomerCountryID = prod_country.CountryID
于 2013-01-30T12:52:38.887 回答
0
SELECT
    cco.CountryName as CustomerCountryName, pco.CountryName as ProviderCountryName, ppco.CountryName as ProductCountryName
FROM
    tblCustomer cu
INNER JOIN
    tblProvider pr ON cu.ProvID = pr.ProvID
INNER JOIN
    tblProduct po ON pr.ProdID = po.ProdID
INNER JOIN
    tblCountry cco ON cu.CustomerCountryID = cco.CountryID
INNER JOIN
    tblCountry pco ON pr.ProviderCountryID = pco.CountryID
INNER JOIN
    tblCountry ppco ON po.ProductCountryID = ppco.CountryID

这假设您有一个tblCountryCountryID列调用的表。

于 2013-01-30T12:50:36.547 回答
0

您可以多次加入国家/地区表:

SELECT cuco.CountryName,
       prco.CountryName,
       poco.CountryName
FROM   tblCustomer cu
       INNER JOIN tblProvider pr
         ON cu.ProvID = pr.ProvID
       INNER JOIN tblProduct po
         ON pr.ProdID = po.ProdID 
       INNER JOIN tblCountry cuco
         ON cu.CustomerCountryID = co.CountryID
       INNER JOIN tblCountry prco
         ON pr.ProviderCountryID = co.CountryID
       INNER JOIN tblCountry poco
         ON po.ProductCountryID = co.CountryID
于 2013-01-30T12:51:34.617 回答
0

您将需要某种连接来获得价值,没有其他方法。不过,它不必是与您已经在做的两个相同类型的连接;它可能是一个内联视图:

SELECT cu.CustomerCountryID,
       pr.ProviderCountryID,
       po.CountryName
FROM   tblCustomer cu
,      tblProvider pr
,     (SELECT po.ProdID
       ,      co.CountryName
       FROM   tblProduct po
       ,      tblCountry co
       WHERE  po.ProductCountryID = co.productCountryID
) po
WHERE  cu.ProvID = pr.ProvID
AND    pr.ProdID = po.ProdID;

或者(取决于数据库提供程序)您可以在选择列表中进行子选择:

SELECT cu.CustomerCountryID,
       pr.ProviderCountryID,
       (SELECT co.CountryName FROM tblCountry co WHERE po.ProductCountryID = co.productCountryID) AS CountryName
FROM   tblCustomer cu
,      tblProvider pr
,      tblProduct po
WHERE  cu.ProvID = pr.ProvID
AND    pr.ProdID = po.ProdID;

我只展示了其中一张表的示例,但同样适用于所有三张表。

于 2013-01-30T12:55:41.620 回答
0

你必须为每个 ID 名称添加一个连接......你也可以试试这个;

SELECT 
  (Select CountryName from tblCountry where CountryID =cu.CustomerCountryID),
  (Select CountryName from tblCountry where CountryID =pr.ProviderCountryID),
  (Select CountryName from tblCountry where CountryID =po.ProductCountryID)                
FROM tblCustomer cu    
  INNER JOIN tblProvider pr ON cu.ProvID = pr.ProvID
  INNER JOIN tblProduct po ON pr.ProdID = po.ProdID
于 2013-01-30T13:04:39.843 回答