【发布时间】:2019-03-12 15:17:24
【问题描述】:
在我的数据库中,我有用户、应用程序和版本。一个用户可以通过权限表拥有 0..n 个应用,一个应用可以拥有 0..n 个版本。
我正在尝试获取至少拥有 1 个应用程序的用户列表,但该用户的应用程序都没有任何版本。
架构大致
users permissions apps releases
----- ----------- ---- --------
id user_id id id
email app_id app_id
我认为我有一些东西可以解决这个问题,但对我来说似乎效率低下,因为我提到了两次权限表并且我正在使用嵌套的存在子句。有没有更有效的方法来编写这个查询?
select u.email from users u
join permissions p on p.user_id = u.id
where not exists (
select a.id from apps a
join permissions p on p.app_id = a.id
where p.user_id = u.id and exists (
select r.id from releases r
where r.app_id = a.id
)
);
【问题讨论】:
-
这些答案之一是否解决了您的问题?如果是这样,请将答案标记为已接受(向上/向下投票箭头下方的绿色箭头)。否则,您能否提供更多信息来帮助解决问题。见stackoverflow.com/help/someone-answers
标签: mysql sql performance join exists