我想获取事件ID为event_id ='NGO102' or event_id ='NGO103' or event_id ='NGO109' or event_id ='NGO111'
的数据
这是我的查询
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
但是显示所有记录我需要具有event_id的记录是
event_id ='NGO102' or event_id ='NGO103' or event_id ='NGO109' or event_id ='NGO111'
。我在哪里做错了,请指导我
谢谢
答案 0 :(得分:4)
您可能希望在括号内的每个AND
上有一个OR
条件。
..
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'
)..
您可以使用IN
.. AND event_date <= '2018-04-01 00:00:00'
AND event_id IN ( 'NGO102','NGO103','NGO109','NGO111') ..
答案 1 :(得分:0)
您为什么要使用group by
?我怀疑您的查询可能有问题。
Kaushik指出,您想要in
。您还应该使用含义表别名并限定所有列名称:
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;
请注意,饮食上不需要时间部分。 JOIN
条件也是可疑的。我希望JOIN
位于event_id
之类的地方。