简体   繁体   English

创建插入的条件SQL select语句

[英]Creating a conditional SQL select statement that inserts

So, I've got this table with hierchical data, and I'm trying to select the top-level and insert it into a column called 'TopLevel' 所以,我已经有了这个具有重要数据的表,我正在尝试选择顶级并将其插入名为“TopLevel”的列中

Heres what the data would look like The top org is called Top-Level, and all lower levels have 'Top-Level' as the very top level, eventually. 下面是数据看起来像什么?顶级组织称为顶级组织,所有较低级别最终都将“顶级”作为最高级别。

Organization | Parent Org | GParent Org | GGParent Org | Toplevel
Top-Level | - | - | -| - |
Food Corp | Top-Level | - | -| - |
Taco Bell | Food Corp | Top-Level |  -| - |
Taco Bell Express | TacoBell | Food Corp | Top-Level | - |
KFC | Food Corp | Top-Level | -| - |
Chuck e. Cheese | Top-Level | -| - | -| 

I can't find a close enough example that helps me understand how to do conditional selects/inserts with a string. 我找不到足够接近的示例,这有助于我理解如何使用字符串进行条件选择/插入。

on the condition that Parent Org = Top-Level, Org should be selected and inserted into column top-level. 在父组织=顶级的情况下,应选择组织并将其插入列顶级。 In the database, if something has no more parent organizations, Top-Level appears as the parent. 在数据库中,如果某些内容没有更多父组织,则Top-Level将显示为父组织。 Wherever top-level is found, one level down(so, Food Corp, chuck e. cheese) would be selected and inserted into the column Toplevel. 无论在哪里找到顶层,都会选择一级(因此,Food Corp,chuck e.cheese)并将其插入到Toplevel列中。 Hope that isn't too confusing. 希望不要太困惑。

Here's the semi-english version of the select statement: 这是select语句的半英文版本:

select Org.displayName, Parent.displayName as ParentOrg_Name, GParent.displayName as gParentOrg_Name,GGParent.displayName as ggParentOrg_Name, Org.uid, Org.accountType, Org.orgType,Parent.orgType, GParent.orgType

CASE
          If Org.ParentOrg_Name == Top-Level, 
             then toplevelorg = Org.Displayname
          elseif gParentOrg_Name == Top-Level, 
              then toplevelorg = ParentOrg_Name
         elseif ggparentOrg_Name == Top-Level,
             then toplevelorg = gParentOrg_Name
           else toplevelorg = ggparentOrgName

     from dbo.Organization Org
     LEFT OUTER JOIN dbo.Organization Parent ON Org.parentOrganization_id = Parent.id
     LEFT OUTER JOIN dbo.Organization GParent ON Parent.parentOrganization_id = GParent.id
     LEFT OUTER JOIN dbo.Organization GGParent ON GParent.parentOrganization_id = GGParent.id

MySQL, PHP... A working TopLevel column will help me do a meaningful group-by, as I cannot simply group by parent ID without seperating the top level orgs from the lower level orgs(that they own). MySQL,PHP ......一个有效的TopLevel专栏将帮助我做一个有意义的分组,因为我不能简单地按父ID分组而不分离顶级组织(他们拥有)的顶级组织。

Looking for teach a man to fish response, including an explaination of 'how does the select org.displayname insert into column 'toplevel' work in a statement like this? 寻找教导一个人捕鱼的反应,包括解释'如何选择org.displayname插入列'顶层'工作在这样的声明中? I look at these conditional statements and have trouble understanding the THEN parts'. 我看一下这些条件陈述,并且无法理解这些条件。 Note that I don't know if I should put 'select toplevel' in the top select clause. 请注意,我不知道是否应该在top select子句中加上'select toplevel'。 I look forward to any response 我期待着任何回应

The SQL syntax for CASE is lightly different from yours, but you were close: CASE的SQL语法与您的SQL语法略有不同,但您很接近:

CASE
    WHEN Org.ParentOrg_Name = 'Top-Level' 
         then Org.Displayname
    WHEN gParentOrg_Name = 'Top-Level' 
         then ParentOrg_Name
    WHEN ggparentOrg_Name = 'Top-Level'
         then  gParentOrg_Name
    ELSE ggparentOrgName
END AS toplevelorg

Thus, the full statement would be 因此,完整的陈述将是

select Org.displayName,
       Parent.displayName as ParentOrg_Name,
       GParent.displayName as gParentOrg_Name,
       GGParent.displayName as ggParentOrg_Name,
       Org.uid,
       Org.accountType,
       Org.orgType,Parent.orgType,
       GParent.orgType,
       CASE
           WHEN Org.ParentOrg_Name = 'Top-Level' 
                then Org.Displayname
           WHEN gParentOrg_Name = 'Top-Level' 
                then ParentOrg_Name
           WHEN ggparentOrg_Name = 'Top-Level'
                then  gParentOrg_Name
           ELSE ggparentOrgName
       END AS toplevelorg

     from dbo.Organization Org
     LEFT OUTER JOIN dbo.Organization Parent ON Org.parentOrganization_id = Parent.id
     LEFT OUTER JOIN dbo.Organization GParent ON Parent.parentOrganization_id = GParent.id
     LEFT OUTER JOIN dbo.Organization GGParent ON GParent.parentOrganization_id = GGParent.id

Frank PI's response mostly worked, but I got an error saying mssql_query() (php) didnt recognise the column name ParentOrg_Name and other colum names. Frank PI的响应大部分都有效,但是我得到一个错误,说mssql_query()(php)没有识别列名ParentOrg_Name和其他列名。 I found that this worked, and that the aliases weren't working within the case statement. 我发现这有效,并且别名在case语句中不起作用。 So the full statement is this: 所以完整的陈述是这样的:

select Org.displayName as Organization_Name, Parent.displayName as ParentOrg_Name,
GParent.displayName as gParentOrg_Name,GGParent.displayName as ggParentOrg_Name,
     Org.uid, Org.accountType, Org.orgType,
     CASE WHEN Parent.displayName = 'Top-Level' then Org.Displayname
         when GParent.displayName = 'Top-Level' then Parent.displayName
         when ggParent.displayName = 'Top-Level' then gParent.displayName
         else ggParent.displayName
    END AS toplevelorg
     from HQDevBox.dbo.Organization Org
     LEFT OUTER JOIN HQDevBox.dbo.Organization Parent ON Org.parentOrganization_id = Parent.id
     LEFT OUTER JOIN HQDevBox.dbo.Organization GParent ON Parent.parentOrganization_id = GParent.id
     LEFT OUTER JOIN HQDevBox.dbo.Organization GGParent ON GParent.parentOrganization_id = GGParent.id

To excercise the learning, there was another possible case where I could use CASE, because orgType returns a number, which is mapped to a string value. 为了练习学习,还有另一种可能的情况,我可以使用CASE,因为orgType返回一个数字,它被映射到一个字符串值。 So here's the statement with both CASE's(is possible and works). 所以这是两个CASE的声明(可行和有效)。

select Org.displayName as Organization_Name, Parent.displayName as ParentOrg_Name,
GParent.displayName as gParentOrg_Name,GGParent.displayName as ggParentOrg_Name,
      CASE WHEN Parent.displayName = 'Top-Level' then Org.Displayname
         when GParent.displayName = 'Top-Level' then Parent.displayName
         when ggParent.displayName = 'Top-Level' then gParent.displayName
         else ggParent.displayName
         END AS toplevelorg,
      Org.uid, Org.accountType,
      CASE WHEN Org.orgType = 0 then 'Agency'
           when Org.orgType = 1 then 'Essential'
           when Org.orgType = 2 then 'Power'
           when Org.orgType = 3 then 'Sub_Agency'
           when Org.orgType = 4 then 'Sub_Organization'
           END AS orgType
     from HQDevBox.dbo.Organization Org
     LEFT OUTER JOIN HQDevBox.dbo.Organization Parent ON Org.parentOrganization_id = Parent.id
     LEFT OUTER JOIN HQDevBox.dbo.Organization GParent ON Parent.parentOrganization_id = GParent.id
     LEFT OUTER JOIN HQDevBox.dbo.Organization GGParent ON GParent.parentOrganization_id = GGParent.id

Hopefully this will help someone else. 希望这会帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM