mysql - 基于唯一多列结果集的查询表
问题描述
有一张users
桌子和一张matches
桌子。匹配由一个userId1
and userId2
(和它自己的id
)组成。我正在尝试返回与某个 userId 不匹配的所有用户。
因此,如果您的用户的 id1
为 , 2
, 3
,并且与 , 匹配4
,那么我希望在搜索具有 id 的用户时返回具有 id 的用户。我不期望,因为有匹配,我不期望,因为它是用户本身。我确实期待,因为比赛不是 with ,同样适用于。我也期待,因为它没有在匹配表中找到。5
2|5
2|3
1|2
3
4
5
1
2
1
1
3
1
5
4
我一直在寻找如何解决这个问题,我觉得我需要使用EXIST
or IN
,但我认为我遇到了语法错误,因为我比较的结果数量不一样?
select * from Users u
where not exist (select userId1 from Matches m where u.id = m.userId1) and
where not exist (select userId2 from Matches m where u.id = m.userId2)
请在下面找到示例数据,以及相同的小提琴。
这将是我要复制的设置:
CREATE TABLE `Users` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=257 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;
INSERT INTO `Users` (`id`, `name`, `createdAt`, `updatedAt`)
VALUES
(1, 'Maria', '2021-07-11 00:51:36', '2021-07-11 18:09:58');
INSERT INTO `Users` (`id`, `name`, `createdAt`, `updatedAt`)
VALUES
(2, 'Peter', '2021-07-11 00:51:36', '2021-07-11 18:09:58');
INSERT INTO `Users` (`id`, `name`, `createdAt`, `updatedAt`)
VALUES
(3, 'Sonya', '2021-07-11 00:51:36', '2021-07-11 18:09:58');
INSERT INTO `Users` (`id`, `name`, `createdAt`, `updatedAt`)
VALUES
(4, 'Frank', '2021-07-11 00:51:36', '2021-07-11 18:09:58');
INSERT INTO `Users` (`id`, `name`, `createdAt`, `updatedAt`)
VALUES
(5, 'Johnny', '2021-07-11 00:51:36', '2021-07-11 18:09:58');
CREATE TABLE `Matches` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`userId1` int DEFAULT NULL,
`userId2` int DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;
INSERT INTO `Matches` (`id`, `userId1`, `userId2`, `createdAt`, `updatedAt`)
VALUES
(1, 2, 5, '2021-07-12 21:57:31', '2021-07-12 21:57:31');
INSERT INTO `Matches` (`id`, `userId1`, `userId2`, `createdAt`, `updatedAt`)
VALUES
(2, 2, 3, '2021-07-12 21:57:31', '2021-07-12 21:57:31');
INSERT INTO `Matches` (`id`, `userId1`, `userId2`, `createdAt`, `updatedAt`)
VALUES
(3, 1, 2, '2021-07-12 21:57:31', '2021-07-12 21:57:31');
select * from Users u
where not exist (select userId1 from Matches m where u.id = m.userId1) and
where not exist (select userId2 from Matches m where u.id = m.userId2)
解决方案
您可以使用自连接来执行此操作,该连接Users
将返回 2 个用户的所有可能组合,然后左连接Matches
并过滤掉匹配的行:
SELECT u1.*
FROM `Users` u1
INNER JOIN `Users` u2 ON u1.id <> u2.id
LEFT JOIN `Matches` m ON (m.userId1, m.userId2) IN ((u1.id, u2.id), (u2.id, u1.id))
WHERE m.id IS NULL AND u2.id = ?
更改?
为您搜索的用户 ID。
请参阅演示。
推荐阅读
- java - 为什么 String 列表没有 forEach 方法?
- android - Flutter:为长文本的动态高度的 ButtonBar 中的两个按钮提供相等的宽度
- angular - Angular命名路由器出口抛出错误
- javascript - jQuery每个循环只给出第一个结果
- java - 无法分析我的 Maven 项目。我的 pom.xml 文件中有错误吗?
- .htaccess - .htaccess RewriteRule 中的阿拉伯语 ( م ) 字符问题
- html - 使用引导程序使网站移动的问题
- excel - VBA:查找具有确切日期或更晚日期的第一行
- haxe - 是否可以将 Flashdevelop 项目从 Openfl 移植到 Android?
- android - Android 版本 9 上的 MediaPlayer 的一个奇怪问题 - 空指针异常