来自Join的SQL多重查找

时间:2013-01-30 12:47:38

标签: sql

我的查询看起来像这样:

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。

我现在无法想到如何在不导致子查询的情况下执行此操作

任何人都可以建议以更快的方式做到这一点吗?

由于

5 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

这假设您有一个名为tblCountry的表格,其中包含CountryID列。

答案 2 :(得分: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

答案 3 :(得分: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;

我只显示了其中一个表的示例,但同样可以应用于所有三个表。

答案 4 :(得分: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