【问题标题】:equality checks between 2 fields using SQL使用 SQL 在 2 个字段之间进行相等性检查
【发布时间】:2012-07-08 15:48:08
【问题描述】:

我正在尝试查找父类别。

所以我需要写,

其中CategoryIDParentCategoryID 为0

CategoryID 可能是 30,但如果它的 ParentCategoryID 为 0,那么您知道它的父类别。

这是我目前的 SQL:

SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID
FROM          Nop_Category
WHERE         (Deleted = 0) 
AND           (Published = 1) 
AND           (CategoryID = ParentCategoryID = 0)

【问题讨论】:

    标签: sql sql-server-2008


    【解决方案1】:

    要对两个字段执行相等检查,请使用 AND 运算符并指定字段名两次。

    SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID 
    FROM          Nop_Category 
    WHERE         (Deleted = 0)  
    AND           (Published = 1)  
    AND           (CategoryID = ParentCategoryID AND ParentCategoryID = 0) 
    

    但你也可以这样写并获得相同的结果:

    SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID 
    FROM          Nop_Category 
    WHERE         (Deleted = 0)  
    AND           (Published = 1)  
    AND           (CategoryID = 0 AND ParentCategoryID = 0) 
    

    但是,在您的问题中,您提到 CategoryID 可能是 30,因此您的查询将不起作用。您可能希望省略 CategoryID 或通过参数指定特定的 categoryId:

    SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID, 
    FROM          Nop_Category 
    WHERE         (Deleted = 0)  
    AND           (Published = 1)  
    AND           (CategoryID = @categoryID AND ParentCategoryID = 0) 
    

    编辑:

    所以如果 categoryID 与 CetegoryParentID 相同,我知道它是 孩子。

    通常当我做自相关表时,我使用 NULL 作为 ParentId,它告诉我当前行是父行。如果您使用 0 表示 null,那么 CategoryId 为 30 且 ParentCategoryId 为 30 的记录意味着它既不是子代也不是父代。

    ID  PID  Value
    0   0    Top Level - must exist for referential integrity
    1   0    Child of "Top Level"
    2   0    Another child of "Top Level"
    3   1    Child of "Child of Top Level"
    

    在这个场景中,您只能有 1 个顶级类别,所有其他类别都是子类别(即使您认为 ParentCategoryId 为 0 的父类别,它仍然必须位于 CategoryId 0 下)

    使用 NULL

    ID  PID  Value
    1   Null Top Level cat 1
    2   Null Top Level cat 2
    3   1    Child of "Top Level cat 1"
    4   2    Child of "Top Level cat 2"
    

    在这个场景中,我可以轻松找到所有顶级类别

    SELECT * FROM dbo.Category WHERE pid IS NULL
    

    或者,如果我想要特定类别的顶级类别

    SELECT * FROM dbo.Category WHERE CategoryId = 1 AND ParentCategoryId is null
    

    而且我的参照完整性完全没有问题。

    要查找父级的直接子级,只需传入您要查找的父级的 categoryid:

    SELECT * FROM dbo.Category WHERE ParentCategoryId = 1
    

    【讨论】:

    • 虽然语法正确,但没有多大意义 - CategoryIDParentCategoryID 都是 0。
    • 正确...您可以说 CatId = 0 和 ParentCatId = 0 并获得相同的结果。
    • 如何检查孩子?例如,CategoryID 与 ParentCategoryID 相同
    • @Super1 - CategoryID 和 ParentCategoryID 永远不能相同,否则会破坏层次结构。孩子总是有一个与自己的 ID 不同的 parentId,这就是使它成为孩子的原因。
    • 感谢您花时间帮助我。我只是想在周日晚上学习!在这里发布了这个问题的扩展:stackoverflow.com/questions/11384940/…
    【解决方案2】:
    SELECT        CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID
    FROM          Nop_Category
    WHERE         Deleted = 0
    AND           Published = 1
    AND           ParentCategoryID = 0
    

    【讨论】:

    • 您回答了我的问题,谢谢。但是我该怎么写呢,CategoryID == ParentCategoryID
    • 不考虑 CategoryID
    • 所以如果 categoryID 与 CetegoryParentID 相同,我知道它是一个孩子。
    猜你喜欢
    • 2013-04-24
    • 2020-05-24
    • 2023-04-04
    • 2011-05-04
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多