简体   繁体   English

Select 从另一个表查询

[英]Select query from another table

I have two tables:我有两张桌子:

CREATE TABLE store_category (
    store_id    int  NOT NULL PRIMARY KEY,
    category_id char NOT NULL
)

CREATE TABLE category (
    category_id char NOT NULL PRIMARY KEY,
    parent_id   char     NULL,
    name        char NOT NULL
)
store_id store_id category_id类别ID
1 1 1a 1a
2 2 2b 2b
3 3 3c 3c

category table: this table has 3 levels of category. category表:该表有3个级别的类别。 The highest level of category has NULL in parent_id :最高级别的类别在parent_id中有NULL

category_id类别ID parent_id parent_id name姓名
1a 1a NULL NULL a一个
2b 2b 1a 1a b b
3c 3c 2b 2b c c

I want to get all related category name of store category.我想获取商店类别的所有相关类别名称。

Expected query result:预期查询结果:

store_id store_id category_id类别ID lv1 lv1 lv2 lv2 lv3 lv3
1 1 1a 1a a一个 b b c c
2 2 2b 2b a一个 b b c c
3 3 3c 3c a一个 b b c c

I have an idea, I will get all category names that relate to each category_id in category table into temp table and then join temp table with store_category table.我有一个想法,我将与category表中每个category_id相关的所有类别名称放入临时表中,然后将临时表与store_category表连接起来。 I wonder if that is a good idea.我想知道这是否是个好主意。

I have an idea, I will get all category names that relate to each category_id in category table into temp table and then join temp table with store_category table.我有一个想法,我将与类别表中每个category_id相关的所有类别名称放入临时表中,然后join临时表与store_category表连接起来。 I wonder if that is a good idea.我想知道这是否是个好主意。

It is a good idea - except you don't need a temporary table - nor a loop.一个好主意——除了你不需要临时表——也不需要循环。 All you need is a Recursive (self-referential) CTE.您只需要一个递归(自引用)CTE。 CTEs are how you select data hierarchies and other graph structures. CTE 是 select 数据层次结构和其他图形结构的方式。

However I don't think it's a good idea to PIVOT (or more specifically, UNPIVOT ) your data into those lv1 , lv2 , and lv3 columns - I know your business rules say there will only be 3 levels of depth, but your data-model does allow more than 3 levels of depth (unless you have a CHECK CONSTRAINT with a UDF that uses the same style of CTE query to restrict maximum depth ).但是,我认为将PIVOT (或更具体地说, UNPIVOT )您的数据放入lv1lv2lv3列中不是一个好主意-我知道您的业务规则说只有 3 个深度级别,但是您的数据-模型确实允许超过 3 级深度(除非您有一个带有UDFCHECK CONSTRAINT使用相同样式的 CTE 查询来限制最大depth )。 When you have a dimension of data that varies with a query (like this one,) then you should return them as rows and then format them for display to end-users in your application code.当您的数据维度随查询而变化(例如这个)时,您应该将它们作为行返回,然后将它们格式化以在应用程序代码中显示给最终用户。 not directly in SQL.不是直接在 SQL 中。

As for the hierarchical query: Read this: CTE Recursion to get tree hierarchy至于分层查询:请阅读: CTE Recursion to get tree hierarchy

Something like this:像这样的东西:

DECLARE @getThisCategoryId char(2) = '3c';

WITH my_cte AS (
    
    SELECT
        c.category_id,
        c.parent_id,
        c.name,
        1 AS depth
    FROM
        category AS c
    WHERE
        c.category_id = @getThisCategoryId 

    UNION ALL
    
    SELECT
        other_categories.category_id,
        other_categories.parent_id,
        other_categories.name,
        collected_so_far.depth + 1 AS depth
    FROM
        category AS other_categories
        INNER JOIN my_cte AS collected_so_far ON
            other_categories.category_id = collected_so_far.parent_id    
)
SELECT
    *
FROM
    my_cte
    INNER JOIN store_category AS sc ON my_cte = category_id
ORDER BY
    sc.store_id

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

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