这是架构:
tblCustomers{custid,fname,lname,email}
tblTicket{ticketId,custid,numOfPassengers,dateofJourney,totafare,trainId}
问题查询是:显示已预订多个故障单的所有客户。我正在使用Oracle 9.两个表的custid之间有一个外键。
答案 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)