zoukankan      html  css  js  c++  java
  • SQL 有父标识的 递归查询

    递归查询,临时表的高级应用

    WITH temp
    AS
    (
    --父项
    SELECT * FROM Ar_Area WHERE Ar_Parent = 1
    UNION ALL
    --递归结果集中的下级
    SELECT m.* FROM Ar_Area AS m
    INNER JOIN temp AS child ON m.Ar_Parent = child.Ar_Code
    )
    SELECT * FROM temp

    实际应用:

    只查一个父ID的所有子分类包括自己

     WITH temp 
    AS
    (
    --父项
    SELECT * FROM tg_ProductCategory WHERE CategoryKey = 'BaiGe'
    UNION ALL
    --递归结果集中的下级
    SELECT m.* FROM tg_ProductCategory AS m
    INNER JOIN temp AS child ON m.Parentid = child.ProductCategoryid
    )
    SELECT ProductCategoryId,ParentId,ProductCategoryName FROM temp

    查询结果如图:

    如果查某商品是否属于跟节点【特价商品】的就用

     WITH temp 
    AS
    (
    --父项
    SELECT * FROM tg_ProductCategory WHERE CategoryKey = 'BaiGe'
    UNION ALL
    --递归结果集中的下级
    SELECT m.* FROM tg_ProductCategory AS m
    INNER JOIN temp AS child ON m.Parentid = child.ProductCategoryid
    )
    select * from tg_GroupProduct where NewCategoryId in(
    SELECT ProductCategoryId FROM temp) and GroupProductId= 1232
  • 相关阅读:
    拥抱函数式编程 I
    关于CMS的那点事 I
    常用正规表达式
    javascript source map 的使用
    架构师修炼 后记
    CSS 天坑 I
    架构师修炼 III
    架构师修炼 II
    win10,VM14 安装cnetos6.9 虚拟机黑屏和只有光标闪烁解决办法
    C/C++数组初始化
  • 原文地址:https://www.cnblogs.com/lizeyan/p/3941756.html
Copyright © 2011-2022 走看看