MYSQL数据库查询:建议用户关注
我希望在一个允许用户相互关注的系统中实现类似Twitter的“关注谁”的建议功能。我使用如下表跟踪这些订阅:MYSQL数据库查询:建议用户关注,mysql,Mysql,我希望在一个允许用户相互关注的系统中实现类似Twitter的“关注谁”的建议功能。我使用如下表跟踪这些订阅: CREATE TABLE `subscriptions` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `follower_id` int(10) unsigned NOT NULL, `following_id` int(10) unsigned NOT NULL, `enabled` tinyint(1) NOT N
CREATE TABLE `subscriptions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`follower_id` int(10) unsigned NOT NULL,
`following_id` int(10) unsigned NOT NULL,
`enabled` tinyint(1) NOT NULL DEFAULT '0',
)
我正在尝试获取当前用户尚未跟踪的用户列表
到目前为止,我已尝试使用以下查询:
SELECT DISTINCT u.id, u.name, s.follwer_id, s.enabled
FROM users u
LEFT JOIN subscriptions s ON u.id = s.follwer_id AND s.enabled = 0
WHERE u.id != 2
这个查询最终给了我很多空值和不相关的行
提前感谢。在您的查询中,
用户
表指的是被跟踪的用户。您希望将follower\u id
设置为当前用户,并将follower\u id
重新匹配到用户(对于左连接
):
请注意,最后两列始终为
NULL
,因为您正在寻找没有匹配订阅记录的用户。感谢您向我指出NULL列背后的原因,我从中了解到了这一点。
SELECT u.id, u.name, s.follower_id, s.enabled
FROM users u LEFT JOIN
subscriptions s
ON u.id = s.following_id AND s.enabled = 0 and s.follower_id = 2
where s.following_id is null