我试图加入的三个查询,2个表和一个计数连接本身工作正常:
SELECT * FROM `AccountInfo` NATURAL JOIN `userStats` WHERE `username` = 'x'
我想加入:
SELECT COUNT(following) FROM `followers` WHERE `following` = 'x'
为了获得跟随特定用户的关注者数量,但我无法让加入工作。有什么想法吗?
Account info:
|username|firstname|sirname |address |postcode|
|--------|-------- |--------|--------|--------|
| user1 | first | last |road A | 1234 |
| user2 | name2 | last2 |road b | 4567 |
| | | | | |
| | | | | |
userStats
|username|followers|following|lastseen |reputation|
|--------|-------- |-------- |-------- |-------- |
| user1 | | |DD:MM:YY | 120 |
| user2 | | | DD:MM:YY| 130 |
| | | | | |
| | | | | |
followers
|follower|following|
|--------|-------- |
| user1 | user 2 |
| user2 | user 1 |
| user1 | user 3 |
| | |
自然连接成功附加了accountInfo和userStats,但我还要计算用户拥有的关注者数量,即用户名称出现在""列中的次数,以及用户数量他遵循,即用户名出现在"关注者"中的次数。 (虽然我没有问过后一部分)
对于样本数据,它将显示:follow = 2和followers = 1
我不介意数据是在单独的列中出现在连接表的末尾,但理想情况下会取代userStats表中的以下/关注者数据
答案 0 :(得分:0)
鉴于以下数据:
CREATE TABLE `accountInfo` (
`username` varchar(255) DEFAULT NULL,
`firstname` varchar(255) DEFAULT NULL,
`sirname` varchar(64) DEFAULT NULL,
`address` varchar(64) DEFAULT NULL,
`postcode` varchar(64) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `userstats` (
`username` varchar(255) DEFAULT NULL,
`followers` varchar(255) DEFAULT NULL,
`following` varchar(64) DEFAULT NULL,
`lastseen` date DEFAULT NULL,
`reputation` int(64) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `followers` (
`follower` varchar(255) DEFAULT NULL,
`following` varchar(64) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `accountInfo` (`username`, `firstname`, `sirname`, `address`, `postcode`)
VALUES
('user1', 'first', 'last', 'road a', '1234'),
('user2', 'name2', 'last2', 'road b', '4567'),
('user3', 'name3', 'last3', 'road c', '8974');
INSERT INTO `userstats` (`username`, `followers`, `following`, `lastseen`, `reputation`)
VALUES
('user1', NULL, NULL, '2015-09-22', 120),
('user2', NULL, NULL, '2015-09-21', 130),
('user3', NULL, NULL, '2015-09-20', 80);
INSERT INTO `followers` (`follower`, `following`)VALUES
('user1', 'user2'),
('user2', 'user1'),
('user1', 'user3');
这些查询将提供用户的关注者数量或关注用户的数量:
select a.*, u.*, count(*) as number_following
from accountinfo a
left join userstats u on(a.username = u.username)
left join followers f1 on (u.username = f1.follower)
where 1
group by f1.follower;
select a.*, u.*, count(*) as number_of_followers
from accountinfo a
left join userstats u on(a.username = u.username)
left join followers f1 on (u.username = f1.following)
where 1
group by f1.following;
(2015年9月26日更新) 您可以在左连接中使用子查询与group by来拉取计数:
select a.*, u.*, f1.*, f2.*
from accountinfo a
left join userstats u on(a.username = u.username)
left join (select f.follower as username, count(*) as following_count from followers f group by f.follower) f1 on (u.username = f1.username)
left join (select f.following as username, count(*) as followers_count from followers f group by f.following) f2 on (u.username = f2.username)
where 1;
答案 1 :(得分:0)
不清楚您的输入和所需输出是什么,但我们说我们有以下谓词(参数化语句):
// user `username` named `firstname` `surname` lives at ...
// == `AccountInfo`(`username`,`firstname`,`surname`,...)
// user `username` was last seen at `lastseen` and ...
// == `userStats`(`username`,`lastseen`,...)
// user `follower` follows user `following`
// == `followers`(`follower`,`following`)
(我的userStats
没有followers
& following
个计数列。)
在每个谓词(参数化语句)之后,我写了一个简写。每个速记都像SQL CREATE TABLE
。表(基本或查询结果)包含从其谓词生成真正命题(语句)的行。
(我的回答会继续使用问题中的NATURAL JOIN
。)
满足两个基表谓词的AND的行表是其表的NATURAL JOIN
:
// user `username` named `firstname` `surname` lives at ...
// AND user `username` was last seen at `lastseen` and ...
// == `AccountInfo`(`username`,`firstname`,`surname`,...)
// AND `userStats`(`username`,`lastseen`,...)
SELECT * FROM `AccountInfo` NATURAL JOIN `userStats`
满足基表谓词的AND和条件的行是基表WHERE
条件:
// `AccountInfo`(`username`,`firstname`,`surname`,...)
// AND `userStats`(`username`,`lastseen`,...)
// AND `username` = 'x'
SELECT * FROM `AccountInfo` NATURAL JOIN `userStats` WHERE `username` = 'x'
现在每个用户的关注者和关注者:
// user `username` follows user `following`
// == `followers`(`username`,`following`)
SELECT `follower` AS `username` FROM `followers`
// user `username` follows `followers` users
CREATE TABLE `nfollowing`
SELECT `follower` AS `username`, COUNT(`following`) AS following
FROM followers
GROUP BY `follower`
// user `username` has follower `follower`
// == user `follower` follows user `username`
// == followers(`follower`,`username`)
SELECT `following` AS `username` FROM `followers`
// user `username` has `followers` followers
CREATE TABLE `nfollowers`
SELECT `following` AS `username`, COUNT(`follower`) AS followers
FROM followers
GROUP BY `following`
我们希望行满足这些谓词的AND,这些行是其表的NATURAL JOIN
中的行:
// user `username` has `followers` followers and follows `following` users
// == user `username` has `followers` followers
// AND user `username` follows `following` users
CREATE TABLE `nfollow`
SELECT * FROM `nfollowers` NATURAL JOIN `nfollowing`
现在你的userStats是:
SELECT * FROM `userStats` NATURAL JOIN `nfollow`
您想要的查询可能是:
SELECT * FROM
`AccountInfo` NATURAL JOIN `userStats` NATURAL JOIN `nfollow`
WHERE `username` = 'x'