2017-02-23 91 views
1

我有兩個SQL SELECT語句,說如何將兩個SQL SELECT語句結合

SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood FROM FoodTable WHERE FoodID IS NOT NULL AND FoodDate >= '2015-10-01' GROUP BY FoodID ORDER BY NumberOfFood DESC 

我也有另外一個SQL語句

SELECT FoodSellerID, Market1, SellerLastName, SellerFirstName, PrimaryAddress1, PrimaryAddress2, PrimaryCity, PrimaryState, PrimaryZip FROM SellerTable, MarketTable 
WHERE Market1= MarketTable.MarketID 

幾個音符指出,FoodID查找到SellerTable,市場1查找到MarketTable。我的問題是我怎樣才能結合並返回表格,如

FoodID |食物數量| SellerLastName | SellerFirstName | PrimaryAddress1 | PrimaryAddress2 | PrimaryCity | PrimaryState |基於FoodID查找您的模糊描述SellerTable PrimaryZip

+1

請格式化您的查詢,現在完全無法讀取。 – HoneyBadger

+1

請僅標記相關的DBMS – JohnHC

+3

您應該開始使用ANSI-92樣式的連接......它們現在已經使用了超過25年。這意味着使用join關鍵字,而不是在where語句中使用equals謂詞。 –

回答

2

曼尼,你將無法加入這兩個沒有共同領域的兩個查詢。如果你有一個共同的領域,那麼你可以建立每個臨時表並加入第三個查詢。它看起來像FoodTable中的'Food ID'(來自第一個查詢)。如果存在於SellerTable上,那麼您可以加入這兩個(並且加入MarketTable以從該表中獲取行)。

這將是這個樣子:

create temp table food_nums_temp as 
    SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood 
    FROM FoodTable 
    WHERE FoodID IS NOT NULL 
    AND FoodDate >= '2015-10-01' 
    GROUP BY FoodID 
    ORDER BY NumberOfFood DESC 
    ; 

    Create temp table seller_temp as 
    SELECT FoodSellerID, FoodID, 
    Market1, 
    SellerLastName, 
    SellerFirstName, 
    PrimaryAddress1, 
    PrimaryAddress2, 
    PrimaryCity, 
    PrimaryState, 
    PrimaryZip 
    FROM SellerTable, MarketTable 
    WHERE Market1= MarketTable.MarketID 
    ; 

    select a.*, b.SellerLastName 
    , b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2 
    , b.PrimaryCity, b.PrimaryState, b.PrimaryZip 
    from food_nums_temp a 
    inner join seller_temp b on a.FoodID = b.FoodID 

編輯:明知完全DB訪問(創建臨時表)是不允許的,允許使用「與」(又名公共表表達式或CTE),以現階段的數據在我們所需的查詢之前:

with 
    food_nums_temp as (
    SELECT TOP (150) FoodID, COUNT(*) AS NumberOfFood 
    FROM FoodTable 
    WHERE FoodID IS NOT NULL 
    AND FoodDate >= '2015-10-01' 
    GROUP BY FoodID 
    ORDER BY NumberOfFood DESC 
    ),  

    seller_temp as 
    SELECT FoodSellerID, FoodID, 
    Market1, 
    SellerLastName, 
    SellerFirstName, 
    PrimaryAddress1, 
    PrimaryAddress2, 
    PrimaryCity, 
    PrimaryState, 
    PrimaryZip 
    FROM SellerTable, MarketTable 
    WHERE Market1= MarketTable.MarketID 
    ) 

    select a.*, b.SellerLastName 
    , b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2 
    , b.PrimaryCity, b.PrimaryState, b.PrimaryZip 
    from food_nums_temp a 
    inner join seller_temp b on a.FoodID = b.FoodID 
+0

請注意,我沒有訪問完整的數據庫,我只看到,我試圖從三個不同的表中拉出數據.... –

+0

曼尼,我編輯答案使用通用表表達式(CTE,完成使用關鍵字'WITH')在查詢之前將數據分級。請參閱上面的內容。 – kltft

+0

感謝您的幫助,最終得到它的工作。 –

0

,試試這個

SELECT FoodSellerID, 
     Market1, 
     SellerLastName, 
     SellerFirstName, 
     PrimaryAddress1, 
     PrimaryAddress2, 
     PrimaryCity, 
     PrimaryState, 
     PrimaryZip 
FROM SellerTable ST 
INNER JOIN MarketTable MT 
    ON ST.Market1 = MT.MarketID 
inner join 
    (
    SELECT FoodID, COUNT(*) AS NumberOfFood 
    FROM FoodTable 
    WHERE FoodID IS NOT NULL 
    AND FoodDate >= '2015-10-01' 
    GROUP BY FoodID 
    ) a1 
    on a1.FoodID = ST.FoodID 
+0

感謝評論我最終得到它的工作。 –

0

嘗試這樣的:

SELECT TOP (150) a.FoodID, COUNT(*) AS a.NumberOfFood,b.SellerLastName, b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2, b.PrimaryCity, b.PrimaryState, b.PrimaryZipFROM FoodTable a inner join SellerTable b on a.FoodID = b.FoodSellerID WHERE a.Market1= b.MarketTable.MarketID IS NOT NULL AND FoodDate >= '2015-10-01' GROUP BY a.FoodID, b.SellerLastName, b.SellerFirstName, b.PrimaryAddress1, b.PrimaryAddress2, b.PrimaryCity, 
b.PrimaryState, b.PrimaryZip ORDER BY a.NumberOfFood DESC 

注:可能是另一種解決方案。