2
SELECT C.CarID, C.Make, Y.Year, M.Model , e.Engine
FROM Car C 
INNER JOIN CarYear CY ON CY.CarID = C.CarID
INNER JOIN Year y     ON y.YearID = CY.YearID
INNER JOIN Model m    ON m.CarID = C.CarID
INNER JOIN Engine e   ON e.ModelId = m.ModelID

我是 SQL SERVER 的新手,请告诉我如何解决这个问题。我将所有表格分开,汽车、年份、型号、引擎。我用交叉连接填充桥表 dbo.CarYear & dbo.ModelYear,现在我有这个查询,结果是:

1   Acura   1999    ILX 1.5 L LR ELECT/GAS
1   Acura   1999    ILX 2.0 L L4 SOHC
1   Acura   1999    ILX 2.4 L L4 DOHC
1   Acura   1999    RDX 3.5 L V6
1   Acura   1999    RLX 3.5 L V6
1   Acura   1999    MDX 3.7 L V6
1   Acura   2000    ILX 1.5 L LR ELECT/GAS
1   Acura   2000    ILX 2.0 L L4 SOHC
1   Acura   2000    ILX 2.4 L L4 DOHC
1   Acura   2000    RDX 3.5 L V6
1   Acura   2000    RLX 3.5 L V6
1   Acura   2000    MDX 3.7 L V6
1   Acura   2001    ILX 1.5 L LR ELECT/GAS
1   Acura   2001    ILX 2.0 L L4 SOHC
1   Acura   2001    ILX 2.4 L L4 DOHC
1   Acura   2001    RDX 3.5 L V6
1   Acura   2001    RLX 3.5 L V6
1   Acura   2001    MDX 3.7 L V6
1   Acura   2002    ILX 1.5 L LR ELECT/GAS
1   Acura   2002    ILX 2.0 L L4 SOHC
1   Acura   2002    ILX 2.4 L L4 DOHC
1   Acura   2002    RDX 3.5 L V6
1   Acura   2002    RLX 3.5 L V6
1   Acura   2002    MDX 3.7 L V6
1   Acura   2003    ILX 1.5 L LR ELECT/GAS
1   Acura   2012    ILX 1.5 L LR ELECT/GAS
1   Acura   2012    ILX 2.0 L L4 SOHC
1   Acura   2012    ILX 2.4 L L4 DOHC
1   Acura   2012    RDX 3.5 L V6
1   Acura   2012    RLX 3.5 L V6
1   Acura   2012    MDX 3.7 L V6
1   Acura   2013    ILX 1.5 L LR ELECT/GAS
1   Acura   2013    ILX 2.0 L L4 SOHC
1   Acura   2013    ILX 2.4 L L4 DOHC
1   Acura   2013    RDX 3.5 L V6
1   Acura   2013    RLX 3.5 L V6
1   Acura   2013    MDX 3.7 L V6

如您所见,任何年份的所有讴歌都有相同的型号,例如,我如何在填充桥牌表之前消除一些汽车型号,(例如,讴歌 ILX 仅从 2013 年开始)

4

4 回答 4

1

我认为您可以在填充您的表的查询中添加一个 WHERE 子句:

WHERE not (c.Make='Acura' and m.Model='ILX' and y.Year=2013)
于 2013-09-19T19:44:06.540 回答
0

尝试一下,基于假设

Select *  
From CarYear C  
             CROSS JOIN ModelYear M  

WHERE C.KEY NOT IN( Select C1.Key 
                from CarYear C1 
                        inner join ModelYear M1 
                              on C1.Key=M1.Key 
                Where C1.Name='ACura' and C1.Variant='ILX' AND M1.Year=2013 )
于 2013-09-19T19:43:37.803 回答
0

这是一种“基于集合”的方法……您可以将项目放在#DontWantEm 表中,而不是使用大量的 AND、OR 条件。

我不是说 AND,OR 条件是一种无效的方法。如果您需要手动挑选“不想要”的物品,我说这是一种替代方法。

Use Northwind
GO


IF OBJECT_ID('tempdb..#MasterTable') IS NOT NULL
begin
        drop table #MasterTable
end


IF OBJECT_ID('tempdb..#DontWantEm') IS NOT NULL
begin
        drop table #DontWantEm
end



CREATE TABLE #MasterTable 
( 
    [CustomerID] [nchar](5) NULL,
    [OrderID] [int] NOT NULL,
    [ProductID] [int] NULL
)




CREATE TABLE #DontWantEm 
( 
    [CustomerID] [nchar](5) NULL,
    [OrderID] [int] NOT NULL,
    [ProductID] [int] NULL
)


INSERT INTO #MasterTable ( CustomerID , OrderID , ProductID) 
select c.CustomerID , o.OrderID , od.ProductID
from 
dbo.Customers c 
join dbo.Orders o on c.CustomerID = o.CustomerID 
join dbo.[Order Details] od on o.OrderID = od.OrderID

INSERT INTO #DontWantEm ( CustomerID , OrderID , ProductID) 
select top 300 CustomerID , OrderID , ProductID
from #MasterTable


select * from #MasterTable mt
where not exists (select null from #DontWantEm dwe where
dwe.CustomerID = mt.CustomerID and dwe.OrderID = mt.OrderID and dwe.ProductID = mt.ProductID )



IF OBJECT_ID('tempdb..#DontWantEm') IS NOT NULL
begin
        drop table #DontWantEm
end

IF OBJECT_ID('tempdb..#MasterTable') IS NOT NULL
begin
        drop table #MasterTable
end
于 2013-09-19T19:51:18.443 回答
0
SELECT C.CarID, C.Make, Y.Year, M.Model 
FROM Car C
JOIN CarYear CY ON CY.CarID = C.CarID
JOIN Year y ON y.YearID = CY.YearID
JOIN Model m ON m.CarID = C.CarID
GROUP BY C.CarID, C.Make, Y.Year, M.Model 
于 2013-09-19T19:46:02.037 回答