简体   繁体   English

内联两张桌子

[英]Inner Joining with two tables

I need a little help setting up my query. 我需要一些帮助来设置查询。 I'm simply trying to access the amount of people who are in the same 'clan' by joining these two tables together, clan, users. 我只是试图通过将这两个表(氏族)连接在一起来访问处于同一“氏族”中的人数。 Each users has a column 'clan' which is the same as the table clan's column 'roomOwner' and then I'm trying to get the table clan's information along with the amount of members so it would be like: room, roomOwner, members So basically all I have is this: 每个用户都有一个列'clan',与表氏族的'roomOwner'列相同,然后我试图获取表氏族的信息以及成员数量,因此就像:room,roomOwner,members基本上我所拥有的是:

SELECT c.*, count(u.clan) AS members FROM clans c inner join users u WHERE c.roomOwner = u.clan ORDER BY members;

It only shows one clan though. 虽然它只显示一个氏族。 Any help please? 有什么帮助吗?

Your query has no GROUP BY clause. 您的查询没有GROUP BY子句。 and I think it's only returning single record right? 而且我认为它只是返回单条记录,对吗? LEFT JOIN is needed here since there are possibilities that a clan has no member. 这里需要LEFT JOIN ,因为有可能clan没有成员。

SELECT  b.roomOwner, COUNT(a.clan) memberCount
FROM    clan b
        LEFT JOIN users a
            ON a.clan = b.roomOwner
GROUP BY b.roomOwner
ORDER BY memberCount 

You forgot GROUP BY. 您忘记了GROUP BY。 Do you have some "id" column in "clans" table? 在“氏族”表中是否有一些“ id”列? Group by that "id" 按“ id”分组

SELECT c.*, count(u.clan) AS members 
FROM clans c 
inner join users u ON c.roomOwner = u.clan
GROUP BY clans.id

And you need LEFT JOIN there instead of INNER JOIN if you want to see info about all clans, even having 0 users. 如果您想查看所有氏族的信息,即使有0位用户,也需要LEFT JOIN而不是INNER JOIN。

也许这会有所帮助:从氏族中选择c。*,count(links.id)作为成员c左加入用户u在c.roomOwner = u,clan按u.clan按成员顺序排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM