SQL查询以获取具有多个票证的客户

时间:2013-01-07 10:11:52

标签: sql oracle oracle9i

这是架构:

 tblCustomers{custid,fname,lname,email}
 tblTicket{ticketId,custid,numOfPassengers,dateofJourney,totafare,trainId}

问题查询是:显示已预订多个故障单的所有客户。我正在使用Oracle 9.两个表的custid之间有一个外键。

2 个答案:

答案 0 :(得分:2)

此?

SELECT C.custid
FROM tblCustomers C
  JOIN tblTickets T
    ON C.custid = T.custid
GROUP BY C.custid
HAVING COUNT(*) > 1

如果你想让tblCustomers的所有字段都使用它:

SELECT C.custid,C.fname,C.lname,C.email
FROM tblCustomers C
  JOIN tblTickets T
    ON C.custid = T.custid
GROUP BY C.custid,C.fname,C.lname,C.email
HAVING COUNT(*) > 1

SELECT
       C.custid,
       MAX(C.fname) as fname,
       MAX(C.lname) as lname,
       MAX(C.email) as email
FROM tblCustomers C
  JOIN tblTickets T
    ON C.custid = T.custid
GROUP BY C.custid
HAVING COUNT(*) > 1

答案 1 :(得分:1)

select  * 
from    tblCustomers 
where   custid in (select custid from tblTicket group by custid having count(*) > 1)