我有 3 张桌子:
Create table users (
id Number NOT NULL ,
groupID Number NOT NULL ,
name Varchar2(50),
primary key (id)
);
Create table friends (
friendID Number NOT NULL ,
user_a Varchar2(35),
user_b Varchar2(35),
primary key (friendID)
);
Create table groups (
groupID Number NOT NULL ,
gname Varchar2(35),
primary key (groupID)
);
Alter table users add foreign key (groupID) references groups (groupID);
嵌入数据:
Users table
id | groupid | name
--------------------
1 | 1 | s1
2 | 3 | s2
3 | 2 | s3
4 | 1 | s4
5 | 2 | s5
friends table
friendID | user_a | user_b
--------------------------
1 | 1 | 2
2 | 3 | 1
3 | 4 | 2
4 | 5 | 3
5 | 2 | 5
groups table
groupid | groupname
--------------------
1 | in01
2 | in02
3 | in03
我想运行这个查询:任何组之间的关系最多?这是我当前的查询,但它不起作用
SELECT g.gname ,count(a.aname) as con
FROM users a
left join group g on g.groupid=a.groupid
left join friends f on f.user_a=a.id
group by con;