从单列中选择多行

时间:2014-03-10 10:12:01

标签: sql

我有两张桌票和门票活动

t-number  t_street, t_zone,name
888        xyz           abc
999         ddd          rrr
1000        eee          fff 

ticket _events

te_system_ref    te_event_code te_event          te_userid
888               13000         off hold          autodrop
888               11000         disocunt          autodrop
888               10000         on hold              autodrop
999                13000        offfhold          smrithi
1000               11000        disocunt          keerthi

我需要找出哪个te_system_ref同时具有te_events 13000和11000

SELECT  *
  FROM ticket_events as a 
  inner join tickets as b on
 b.t_number = a.te_system_ref
   where 
  te_event_code = '13000'  and //te_event_code in('13000','11000') or te_event in ('off hold' , 'disocunt')
  te_userid = 'AUTODROP'  

3 个答案:

答案 0 :(得分:0)

您可以在此处使用 exists 构建

select *
  from te_system_ref tsr 
         inner join
       tickets t 
         on (tsr.te_system_ref = t.t_number)
 where exists (select 1
                 from tickets_events te
                where te.te_system_ref = t.t_number and
                      te.te_event_code = 11000) and
       exists (select 1
                 from tickets_events te
                where te.te_system_ref = t.t_number and
                      te.te_event_code = 13000)

答案 1 :(得分:0)

你可以做到

Select te_system_ref
From ickets _events
Where te_event_code in('13000','11000')
      And te_userid = 'AUTODROP'
Group By te_system_ref
Having Count(*)=2

答案 2 :(得分:0)

试试这个:

Select te_system_ref
From table1
Where te_userid = 'AUTODROP'
Group By te_system_ref
Having sum((case when te_event_code='13000' then 1 else 0 end)+
           (case when te_event_code='11000' then 1 else 0 end))=2

fiddle