2016-11-25 33 views
-2

我想要連接的輸出中有兩列。我只得到一個,storeID。 StoreComponentID不在那裏。這個聯盟爲什麼不給我兩列?

Here are the two tables, the query, and the results of the query

+0

http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images問題提問/ 285557#285557 –

+0

你的願望輸出是什麼?兩列並排?或一列',空',然後'空,列'?請閱讀[** How-to-Ask **](http://stackoverflow.com/help/how-to-ask) \t \t這裏是[** START **](http ://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/),瞭解如何提高您的問題質量並獲得更好的答案。 –

回答

3

如果你希望兩個欄需要聲明的兩列

SELECT column1, NULL as column2 -- even when Table1 doesnt have column2 
FROM Table1 
UNION 
SELECT NULL as column1, column2 -- even when Table2 doesnt have column1 
FROM Table2 

現在如果你想通過側某種合併的一面。

WITH idA as (
    SELECT StoreComponentID, 
      ROW_NUMBER() OVER (ORDER BY StoreComponentID) as rn 
    FROM StoreComponent 
), idB as (
    SELECT StoreID 
      ROW_NUMBER() OVER (ORDER BY StoreID) as rn 
    FROM Store 
) 
SELECT idA.StoreComponentID, 
     idB.StoreID 
FROM idA 
FULL JOIN idB 
    ON idA.rn = idB.rn 
0

我想出了一個簡單的解決方案:

select S.storeid as sID, SC.storecomponentid as SCID from tstore as S, tstorecomponent as SC 

The results I wanted