我已經創建了下面的查詢:加入的ID或空並獲得第一個結果
select * from store str
left join(
select * from schedule sdl
where day = 3
order by
case when sdl.store_id is null then (
case when sdl.strong is true then 0 else 2 end
) else 1 end, sdl.schedule_id desc
) ovr on (ovr.store_id = str.store_id OR ovr.store_id IS NULL)
的樣本數據:
STORE
[store_id] [title]
20010 Shoes-Shop
20330 Candy-Shop
[SCHEDULE]
[schedule_id] [store_id] [day] [strong] [some_other_data]
1 20330 3 f 10% Discount
2 NULL 3 t 0% Discount
我想從LEFT JOIN
獲得對於兩種數據NULL store_id(全局計劃條目 - 影響所有商店條目)或給定store_id的實際數據。
像這樣加入查詢,返回結果的正確順序,但是對於NULL和store_id匹配。使用連接子句上的OR語句是有意義的。
預期的結果:
[store_id] [title] [some_other_data]
20010 Shoes-Shop 0% Discount
20330 Candy-Shop 0% Discount
當前結果:
[store_id] [title] [some_other_data]
20010 Shoes-Shop 0% Discount
20330 Candy-Shop 0% Discount
20330 Candy-Shop 10% Discount
如果有關於這個問題的更好的方法我很高興能跟隨它。
Thetable定義('\ d tbl'在psql中),你的Postgres版本是in的核心部分形成這樣的問題。和演示數據,實際顯示案件你想要的。 – 2015-04-06 02:06:19