Abhinaw Anand
Abhinaw Anand

Reputation: 41

or coundition in postgres

I want to get the data where event id is event_id ='NGO102' or event_id ='NGO103' or event_id ='NGO109' or event_id ='NGO111'

here is my query

select  event_id, orgname,event_date,num_of_event_pa,start_date
FROM event_details a  
INNER JOIN registration b on a.orgname =b.org_name  
where event_date >= '2017-03-31 00:00:00' AND event_date <= '2018-04-01 00:00:00' or event_id ='NGO102' or event_id ='NGO103' or event_id ='NGO109' or event_id ='NGO111'
 group by a.event_id, a.orgname,a.published_date, b.num_of_event_pa, b.start_date
     order by event_id DESC

but its displaying all record i need record which have event_id is event_id ='NGO102' or event_id ='NGO103' or event_id ='NGO109' or event_id ='NGO111'. where i'm doing mistake please guide me thanks

Upvotes: 0

Views: 56

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269693

Why are you using group by? I suspect something may be wrong with your query.

As Kaushik points out, you want in. You should also use meaning table aliases and qualify all the column names:

select ed.event_id, ed.orgname, ed.event_date,
       ?.num_of_event_pa, ?.start_date
from event_details ed join  
     registration r
     on ed.orgname = r.org_name  
where ed.event_date >= '2017-03-31' and
      ed.event_date < '2018-04-01'
      ed.event_id in ('NGO102', 'NGO103', 'NGO109', 'NGO111')
order by event_id desc;

Note that the time component is not needed on the ate. The JOIN conditions are also suspicious. I would expect the JOIN to be on something like event_id.

Upvotes: 0

Kaushik Nayak
Kaushik Nayak

Reputation: 31648

You probably want an AND condition on each of those OR within a parentheses.

..
 AND event_date <= '2018-04-01 00:00:00' AND 
(  event_id = 'NGO102' OR event_id = 'NGO103' OR event_id = 'NGO109'
OR event_id = 'NGO111'
)..

You can simplify it using IN

..  AND event_date <= '2018-04-01 00:00:00' 
    AND event_id IN ( 'NGO102','NGO103','NGO109','NGO111') ..

Upvotes: 4

Related Questions