Oracle:与group by的多个连接

时间:2013-11-13 15:43:11

标签: sql oracle

我对SQL不是很熟悉。我正在使用oracle。

以下是示例表:

-----------------------
Table A:
A_ID, A_NAME, A_Address
-----------------------
Table B:
A_ID, B_Name,     
-----------------------
Table C:
B_Name, C_Title
-----------------------

输出应该是 A_ID,A_Name,A_Address,Count(C_Title),

每个B_name对应于C_Title(基本上是查找)

请指导

2 个答案:

答案 0 :(得分:0)

SELECT A_ID, A_Name, A_Address, Count(C_Title)
FROM TableA a 
INNER JOIN TableB b ON a.A_ID = b.A_ID
INNER JOIN TableC c ON b.B_Name = c.B_Name
GROUP BY A_ID, A_Name, A_Address

答案 1 :(得分:0)

SELECT * FROM A;

 A_ID A_NAME     A_ADDRESS
---------- ---------- ----------
     1 RAM        MO
     2 SITA       MI
     3 JANAKI     IL


SELECT * FROM B;

 A_ID B_NAME  
---------- ----------
     1 PAUL
     1 KAPIL
     2 DAVE

SELECT * FROM C;

B_NAME     C_TITLE
---------- ----------
KAPIL      HONDA
KAPIL      MAZDA
KAPIL      ODYSSY
DAVE       BENZE
DAVE       LIMOUSINE

SELECT a.A_ID, a.A_Name, a.A_Address, b.B_Name, Count(C_Title)
FROM A a 
INNER JOIN B b ON(a.A_ID = b.A_ID)
INNER JOIN C c ON(b.B_Name=c.B_Name)
GROUP BY a.A_ID, a.A_Name, a.A_Address, b.B_Name;



A_ID A_NAME     A_ADDRESS  B_NAME     COUNT(C_TITLE)
---------- ---------- ---------- ---------- --------------
     2 SITA       MI         DAVE                    2
     1 RAM        MO         KAPIL                   3

在这里,我认为表C中的B_Name具有信誉值(因为它是外键),A中的A_ID,B中的B_Name是主键。