スキーマは次のとおりです。
tblCustomers{custid,fname,lname,email}
tblTicket{ticketId,custid,numOfPassengers,dateofJourney,totafare,trainId}
問題のクエリは次のとおりです。複数のチケットを予約したすべての顧客を表示します。Oracle9を使用しています。両方のテーブルの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)