gpt4 book ai didi

mysql - MySQL 查询协助 - 省略、分组?

转载 作者:行者123 更新时间:2023-11-29 14:40:33 24 4
gpt4 key购买 nike

进行一些调查并针对日志数据库编写查询。

我已经加入了许多表来带回我需要的数据,但我想稍微清理一下。

查询返回所有用户以及他们在其帐户上启用的功能。

这是我正在尝试清理它的方法:

他们是一个名为“actions”的列,有两种状态:“已添加”和“已删除”

如果用户功能具有“已删除”操作,那么我不想显示该用户的相同功能的任何也标记为“已添加”的行

这可能吗?!

这是我到目前为止所拥有的:

select users.id as site_id, users.company_name, feature_log.featurecode, feature.minimum_account as feature_type, users.account_type as site_type, account_types.name as account, feature_log.action, feature_log.time
from users
inner join
feature_log
on users.id = feature_log.siteid
inner join
feature
on feature_log.featurecode = feature.featurecode
inner join account_types
on users.account_type_INC = account_types.id
where feature.minimum_account != 0
AND feature.minimum_account > users.account_type
AND users.status = 'Y'
ORDER BY feature_log.time DESC

感谢您的支持!

最佳答案

因此,为了“静音”给定用户在任何时间点“删除”的所有功能,您可以在以下子查询上添加(左)联接:

SELECT DISTINCT users.id as siteid, feature_log.featurecode, TRUE as mute_feature
FROM users
INNER JOIN feature_log ON (users.id = feature_log.siteid)
WHERE action = 'removed'

这将是给定用户在某个时间点禁用的功能列表。然后在查询的 WHERE 子句中,添加一个过滤器,如下所示:

AND NOT IFNULL(mute_feature, FALSE)

本质上,这将使您的整个查询变为:

select users.id as site_id, users.company_name, feature_log.featurecode, feature.minimum_account as feature_type, users.account_type as site_type, account_types.name as account, feature_log.action, feature_log.time
from users
inner join
feature_log
on users.id = feature_log.siteid
left join (
SELECT DISTINCT users.id as siteid, feature_log.featurecode, TRUE as mute_feature
FROM users
INNER JOIN feature_log ON (users.id = feature_log.siteid)
WHERE action = 'removed'
) as muted_features ON (feature_log.siteid = muted_features.siteid AND feature_log.featurecode = muted_features.featurecode)
inner join
feature
on feature_log.featurecode = feature.featurecode
inner join account_types
on users.account_type_INC = account_types.id
where feature.minimum_account != 0
AND feature.minimum_account > users.account_type
AND users.status = 'Y'
AND NOT IFNULL(mute_feature, FALSE)
ORDER BY feature_log.time DESC

关于mysql - MySQL 查询协助 - 省略、分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8077683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com