【问题标题】:how can i bring what does not have a table, multi table我怎么能带什么没有桌子,多桌子
【发布时间】:2018-09-04 14:48:06
【问题描述】:

我对 SQL 有疑问,我想在 4 个表(用户、appuserFunctionRelation、组织、GroupCommerce)中执行此操作,其中组织与 GroupCommerce 相关,而 appuserFunctionRelation 与两者相关,而另一个具有用户 ID。

我想做的是带来用户没有的GroupCommerce。我该怎么做?

SELECT  
    [AppUserFunctionsRelation].[user],
    [CommerceGroup].Organization,
    [CommerceGroup].ID AS CommerceGroupID ,
    [CommerceGroup].Description AS CommerceGroupDescription
FROM  [AppUserFunctionsRelation] 
RIGHT JOIN [CommerceGroup]  
    ON   CommerceGroup.Organization = AppUserFunctionsRelation.Organization 
    AND  AppUserFunctionsRelation.[GroupCommerce] = [CommerceGroup].ID
 WHERE  AppUserFunctionsRelation.GroupCommerce IS NULL  
    AND CommerceGroup.Organization IN ( SELECT Organizations.[ID] 
                                        FROM AppUserFunctionsRelation 
                                        INNER JOIN Organizations 
               ON Organizations.ID = AppUserFunctionsRelation.Organization
               WHERE AppUserFunctionsRelation.[User] =22
     )

表格:

User
ID|Description
1 |admin

Organization
ID|Description
1 |TestOrg
2 |TestOrg2

CommerceGroup
ID|Organization|Description
1 |1             |TestGC
2 |1             |TestGC2
3 |1             |TestGC3
4 |2             |TestGC4

AppUserFunctionsRelation
User|organization|CommerceGroup 
1   |1           |1  

预期结果

user|organization|commercegroup
1   | 1          |2
1   | 1          |3

如果您拥有组织的所有业务组,它会是这样的

AppUserFunctionsRelation
User|organization|CommerceGroup 
1   |1           |0

这有点过头了,但我需要这样, 使用 0 我意识到用户拥有组织的所有行业组 编号 1

我想带上该组织的用户没有关联的行业组

谢谢

【问题讨论】:

  • 请提供样本数据和预期结果。
  • 您想要的输出与您的示例数据不一致。看起来用户 1 与组织 1 有关系,组织 1 与除 4 之外的所有商业组都有关系。你能修复你想要的输出或解释你想要做什么。
  • 嗯???这对我来说毫无意义。您的样本中缺少某些内容。但是,如果您要发布详细信息,则应将它们发布在问题中,因为 cmets 的格式化很糟糕。
  • 对不起,我是stackoverFlow的新手,我会编辑它,希望你能帮助我谢谢
  • 你是新来的不是问题。但这一点都不清楚。什么是贸易集团?我越读你的问题,我就越困惑。

标签: sql sql-server linq sql-server-2008


【解决方案1】:

我很确定你想要这样的东西。我对列名做了一些假设。如果您确实在每个表中都有名称 ID,我会敦促您解决此问题。 OrganizationID 不仅仅是一个 ID,因为它在 Organization 表中。是的,这有点多余,但是跨表更改名称的列很难处理。

按照我理解的问题,这应该可以工作。

select *
from Users u
join Organization o on o.OrganizationID = u.UserID
join AppUserFunctionsRelation r on r.UserID = u.UserID and r.OrganizationID = o.OrganizationID
left join CommerceGroup cg on cg.OrganizationID = r.OrganizationID
where u.UserID = 1
    and cg.OrganizationID is null

--编辑--

我想你可以做这样的事情来删除用户表。结果应该几乎相同,但现在这似乎没用,因为你得到的关于用户的所有信息都是一个数字。这样的人很难辨认。

select *
from Organization o
join AppUserFunctionsRelation r on r.OrganizationID = o.OrganizationID
left join CommerceGroup cg on cg.OrganizationID = r.OrganizationID
where cg.OrganizationID is null

【讨论】:

  • 它不会像这样工作 select * from AppUserFunctionsRelation r join Organizations 或 on r.Organization = o.ID join CommerceGroup cg on cg.Organization = r.Organization where r. [User] = 37 我带的是用户已经关联的CommerceGroup,我需要的是带没有特定组织关联的GC
  • 这就是为什么有一个左连接和 where 谓词只得到那些没有匹配的。
  • 在这种情况下与用户的关系我不需要它,我只需要表 AppUserFunctionsRelation 中的用户 id 你将如何获取用户表?
  • 你是什么意思“你将如何获得用户表”?
  • 如果没有 users 表,查询将如何,这就是我的意思
【解决方案2】:

它不会工作

select *
from AppUserFunctionsRelation r
join Organizations or on r.Organization = o.ID
join CommerceGroup cg on cg.Organization = r.Organization
where r. [User] = 37

我带来了用户已经关联的CommerceGroup,我需要的是带来没有特定组织关联的GC

【讨论】:

    猜你喜欢
    • 2018-07-09
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 2014-02-10
    • 2021-07-01
    • 2023-04-11
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多