没有关系的父子的SQL查询

时间:2015-07-31 07:58:48

标签: sql sql-server-2005 parent-child

我需要有关我想要创建的查询的帮助。例如,我们说我有这张表:

Description                     Level     Is_Active
----------------------------------------------------
 (1)Metallic industry products        1          1
 (2)+ Various metal products          2          1
 (3)++  Other metal products          3          1
 (1)Rubber and plastic products       1          1
 (2)+ Rubber products                 2          1
 (2)+ Other rubber products           2          1
 (3)++  Other product types           3          1

其中level指定关系。表中的记录设置为重现树结构。我试图做的是一个查询,它选择所有有活动的表中有孩子的父母。例如,如果金属工业产品的Is_Active列设置为0,我不想显示它及其子(金属产品和其他金属产品)。

同样适用于各种金属产品,如果它不活跃,则不要展示它和它的孩子。我尝试使用相同的表格或使用WITH功能,但遗憾的是我无法找到解决方案。

这是一个更具体的例子。金属工业产品变得不活跃。然后选择结果应为:

Description                     Level     Is_Active
----------------------------------------------------
 (1)Rubber and plastic products       1          1
 (2)+ Rubber products                 2          1
 (2)+ Other rubber products           2          1
 (3)++  Other product types           3          1

或者让我们说金属工业产品的孩子变得不活跃。结果集应如下所示:

Description                     Level     Is_Active
----------------------------------------------------
 (1)Metallic industry products        1          1
 (1)Rubber and plastic products       1          1
 (2)+ Rubber products                 2          1
 (2)+ Other rubber products           2          1
 (3)++  Other product types           3          1

1 个答案:

答案 0 :(得分:0)

好的,我已经去了,它不是很漂亮,但看看你的想法。我使用WHILE循环而不是游标来最小化代码:

// Declare variables that is about to use in for loop.
var i;
var marker = [];
var infowindow = []; 
for(i = 0; i < results.length ; i++) {
  marker[i] = new google.maps.Marker({
    position: results[i].geometry.location,
    map: map
  });

  infowindow[i] = new google.maps.InfoWindow({
    content: "test" + i
  });

  // This IIFE will do what in the function's body, 
  // as infowindow[i] is now passed as a param named `infowindow`,
  // we can ensure the event's target infowindow will be affect by the for-loop's i.
  (function(infowindow) {
    google.maps.event.addListenerOnce(marker, 'click', function() {
      infowindow.open(map,marker);
    }); 
  }(infowindow[i]));
}