这是架构:
tblCustomers{custid,fname,lname,email}
tblTicket{ticketId,custid,numOfPassengers,dateofJourney,totafare,trainId}
问题查询是:显示所有预订多于一张票的客户。我正在使用 Oracle 9。两个表的 custid 之间有一个外键。
这?
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
select *
from tblCustomers
where custid in (select custid from tblTicket group by custid having count(*) > 1)