mysql - 从 3 个表中选择用户尚未绑定的位置
问题描述
我有 3 张桌子
用户信息
ID | 姓名 |
---|---|
1 | 鲍勃 |
2 | 简 |
3 | 汤姆 |
地点
ID | 姓名 |
---|---|
1 | 测试1 |
2 | 测试2 |
3 | 测试3 |
4 | 测试4 |
用户位置
用户身份 | 位置ID |
---|---|
1 | 1 |
1 | 2 |
2 | 3 |
基本上我想要实现的是提取用户尚未绑定的位置名称。
在上面的列表中,Bob 有 2 个位置,分别为“test 1”和“test 2”,但他还没有“test 3”或“test 4”。我只希望数据返回测试 3 和 4,因为 Bob 没有这些数据。
对于 Jane She 只有测试 3 有界,但其余 3 都没有
最初我尝试过这个并且它有点工作。然而,每次另一个用户获得一个无限位置时,它都会从列表中删除。我不确定如何在所有这些中添加用户 ID,因此它仅特定于该用户。
SELECT `name` FROM `locations`
WHERE `id` NOT IN (SELECT `locationID` FROM `user_locations`)
解决方案
创建用户和位置表的笛卡尔积(交叉连接),然后使用外连接允许我们在 user_locations 中查找尚未匹配的行:
select
user_info.ID AS UserID
, locations.ID AS locationID
from user_info
cross join locations
left outer join user_locations on user_info.id = user_locations.userid
and locations.id = user_locations.locationid
where user_locations.userid IS NULL
and user_info.name = 'bob'
MySQL 5.6 架构设置:
CREATE TABLE user_info(
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(4) NOT NULL
);
INSERT INTO user_info(id,name) VALUES (1,'bob');
INSERT INTO user_info(id,name) VALUES (2,'jane');
INSERT INTO user_info(id,name) VALUES (3,'tom');
CREATE TABLE locations(
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(5) NOT NULL
);
INSERT INTO locations(id,name) VALUES (1,'Test1');
INSERT INTO locations(id,name) VALUES (2,'Test2');
INSERT INTO locations(id,name) VALUES (3,'Test3');
INSERT INTO locations(id,name) VALUES (4,'Test4');
CREATE TABLE user_locations(
userID INTEGER NOT NULL
,locationID INTEGER NOT NULL
);
INSERT INTO user_locations(userID,locationID) VALUES (1,1);
INSERT INTO user_locations(userID,locationID) VALUES (1,2);
INSERT INTO user_locations(userID,locationID) VALUES (2,3);
查询 1:
select
user_info.ID AS UserID
, locations.ID AS locationID
from user_info
cross join locations
left outer join user_locations on user_info.id = user_locations.userid
and locations.id = user_locations.locationid
where user_locations.userid IS NULL
order by 1,2
结果:
| UserID | locationID |
|--------|------------|
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 2 | 2 |
| 2 | 4 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
推荐阅读
- java - 在java中的n-ary树中找到一个不包含最大子节点数的节点
- c++ - 打破 While 循环 C++
- javascript - 如何在 React 中从非 React 库中渲染 div 对象?
- git - 将分支恢复到旧位置的最佳方法是什么?
- xml - 修改python-docx中paragraph.style._element.xml中的XML
- javascript - 我想使用 document.getElementByID 自动填充网络表单,但是每当我返回网站时,ID 都会不断变化
- sql - 如何列出 Teradata 中所有定义的宏?
- laravel-5.8 - 如何扩展 FaCuZ/laravel 主题中的布局?
- javascript - 将子组件中的文本字段链接到 VueJS 中父组件的道具
- android - 可以在打印 logcat 之前获取日志吗?