2015-05-28 86 views
0

我在圈子裏跑來跑去,在過去的幾個小時裏一直這樣做。SQL Server查詢不會返回所有商店

我正在做一個選擇在一個表中有每個商店的所有股票,我正在過濾產品ID。我需要的是:即使沒有該商店的任何數據,也要列出所有商店的列表,但該選擇僅返回4個商店或更少。

下面是一個例子:

這是查詢:

select 
    store_id, product_id, start_date, quantity 
from 
    stock 
where 
    product_id = 407214 

這就是結果:

store_id | product_id |  start_date  | quantity | 
    2 | 407214 | 2015-05-26 08:32:53 | 10 | 
    3 | 407214 | 2015-03-16 12:10:00 | 25 | 
    4 | 407214 | 2015-01-06 11:45:15 | 16 | 
    7 | 407214 | 2015-05-14 00:00:00 | 8  | 

而這正是我想要的:

store_id | product_id |  start_date  | quantity | 
    1 | NULL |   NULL   | NULL | 
    2 | 407214 | 2015-05-26 08:32:53 | 10 | 
    3 | 407214 | 2015-03-16 12:10:00 | 25 | 
    4 | 407214 | 2015-01-06 11:45:15 | 16 | 
    5 | NULL |   NULL   | NULL | 
    6 | NULL |   NULL   | NULL | 
    7 | 407214 | 2015-05-14 00:00:00 | 8  | 

我真的需要幫助,這是d讓我瘋狂!

+4

添加到您那裏:或PRODUCT_ID IS NULL – jarlh

+0

什麼都不是男人,返回相同的結果 –

+0

即使我選擇* from stock其中product_id = 407214或product_id爲null,它將返回相同的結果 –

回答

5

的解決方案取決於你的數據庫結構

如果stock表只包含可用的產品,你需要的表storesleft joinstock

select 
    s.id as store_id, 
    st.product_id, 
    st.start_date, 
    st.quantity 
from 
stores s 
left join stock st on s.Id = st.store_id and st.product_id = 407214 
+0

灰,我試圖做你喜歡你發佈,但結果仍然相同 –

+0

@AlanCarvalhoChiganças,請注意,我刪除了where子句並將product_id的過濾器移入連接條件。你是否也這樣做? – ASh

+0

你從哪裏得到有商店表? –

3

添加OR product_id IS NULLWHERE條款

+0

只是一個建議,但也許可以解釋爲什麼解決了這個問題。提問者可能並不明顯。 –

+0

沒有人,返回相同的結果 –

+0

是否是一個實際的NULL(意味着該列可以爲空並且被認爲是emtpy)還是將單詞'NULL'放入列中? – TTeeple

0

Is null將回到那裏是沒有價值的行。 where子句中的or用於條件。如果特定的行意味着這些條件之一。

select store_id, product_id, start_date, quantity 
    from stock where 
      product_id = 407214 OR Product_id iS null 

或者你的null是一個字符串?

select store_id, product_id, start_date, quantity 
    from stock where 
      product_id = 407214 OR Product_id like null 

或者,也許你正在爲空輸入的product_id你可以試試這個:

select store_id, product_id, start_date, quantity 
    from stock where 
     product_id = 407214 OR Product_id = null 

或者,也許,如果你想要的一切,刪除where條款,但您​​的product_id訂購:(也許這就是你意思)

select store_id, product_id, start_date, quantity from stock order by product_id 
+0

...我永遠不會期望這個工作,因爲你不應該在鍵中使用空值。該表的設計與數據(可能)以這種方式存儲相反。 –

0
select store_id, product_id, start_date, quantity from stock where product_id = 407214 or product_id = null 
+0

...我永遠不會期望這個工作,因爲你不應該在鍵中使用空值。該表的設計與數據(可能)以這種方式存儲相反。 –

0

知道如何generate fixed number of rows in a table您可以:

--select 5 as store_id, 407214 as product_id, 3 as start_date, 3 as quantity into stock 

DECLARE @maxStoreId int 
SELECT 
    @maxStoreId = MAX(store_id) 
FROM 
    stock; 

WITH r AS (
    SELECT 1 AS n 
    UNION ALL 
    SELECT n+1 FROM r WHERE n+1<[email protected] 
) 
SELECT 
    r.n as store_id, product_id, start_date, quantity 
FROM 
    r left outer join stock on r.n = stock.store_id 
WHERE 
    product_id = 407214 or product_id is null 

--drop table stock 
+0

如果你有一個商店表,@ ASH解決方案更好 – sbiz

+0

sbiz,我試圖做你喜歡的貼子,但結果仍然是一樣的。列n顯示與store_id –

+0

現在嘗試相同的值。我修改了代碼,使r.n作爲store_id。請記住,store_ids已生成,並且可能不存在於您的數據模型中 – sbiz