postgresql vs mysql left join

时间:2013-05-07 12:16:53

标签: mysql postgresql

我在PostgreSQL数据库上编写查询,并且不确定在连接后如何计算非空值。在MySQL中我会写:

select count(a.clicker_id), count(b.clicker_id) from
(select * from events where type='click') a 
left join (select * from tbl_events where type='conversion')b 
on a.clicker_id=b.clicker_id

基本上,尝试在没有子查询的情况下编写以下内容:

select  date(cl.created_at), count(cl.click_id) as clicks, count(cp.click_id) as Conversions from events_table cl
left outer join (select click_id, created_at from events_table  where type='conversion_potential')cp
on (cl.click_id=cp.click_id)
where cl."type"='click' 
and cl.placement_id in (1,2,3)
group by 1

2 个答案:

答案 0 :(得分:1)

您不需要自我加入:

select
    count(type = 'click' or null) clicks,
    count(type = 'conversion' or null) conversions
from events_table
where campaign_id = 555

答案 1 :(得分:1)

这不起作用:

select count(a.clicker_id), count(b.clicker_id)
    from events_table as a
        left join events_table as b
        on a.clicker_id=b.clicker_id and b.type='conversion'
where a.type='click'
and campaign_id=555