我已经创建了下面的查询:加入的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