【问题标题】:One SQL query to show parent records and their child records?一个显示父记录及其子记录的 SQL 查询?
【发布时间】:2016-04-16 22:07:36
【问题描述】:

我有一个如下所示的树结构

Table1
- Iron
 + Land Vehicle
 + Water Vehicle
 + Air Craft 

Select * from Table1 Where Type = 'Vehicle'  

将给出以下结果。

Land Vehicle
Water Vehicle
Air Craft 

但是展开后的实际结构如下图,

Table1
- Vehicle
 - Land Vehicle
     Car
     Van
     Bus
 - Water Vehicle
     Boat
     Ship
 - Air Craft 
     Jet
     Helicopter

上面的数据库表看起来像,

Item             Type

King Bed          Bed
Double Bed        Bed
Steel D/Bed    Double Bed
Land Vehicle    Vehicle
Car           Land Vehicle 
Van           Land Vehicle 
Bus           Land Vehicle
Water Vehicle   Vehicle
Boat          Water Vehicle 
Ship          Water Vehicle 
Air Craft       Vehicle
Jet            Air Craft
Helicopter     Air Craft 
Arm Chair       Chair

结果应该是所有车辆,结果应该如下所示。

Land Vehicle   
Car           
Van           
Bus          
Water Vehicle   
Boat          
Ship          
Air Craft      
Jet           
Helicopter 

基本上,所需的 Query 需要做的就是选择 Vehicle 类型的所有内容,然后将结果集作为搜索条件并再次选择。例如,如果有三种类型的车辆,下一步应该是一个接一个地拿走这三种,并检查这三种下面是否有任何东西。如果有的话,一一查找,直到树结构结束。

【问题讨论】:

  • 我认为您需要重新格式化项目类型部分以提高可读性,目前很难看到您的表格是什么格式。另外请提供表格的详细信息,例如字段名称。
  • 我知道..我想,但我真的不知道该怎么做。
  • 在节前放4个空格会缩进,按回车键并在每个条目前放4个空格。
  • 嗯,你昨天不是问过这个问题吗? (或者是同班的其他人?)

标签: sql sql-server for-loop while-loop do-while


【解决方案1】:
select distinct(type)
from table1
where type like '%Vehicle%'
or type like '%Craft%';

这样的东西应该可以工作,但是我会在你的桌子设计上工作。类型应该只说“车辆”、“床”、“椅子”等和另一个字段,例如类型 2 应该有“土地”、“空气”、“水”、“家具”或类似的东西。

我个人认为 Type1 为“家具”,Type2 将其识别为“椅子”。

在您最近发表评论之后:

select item, distinct(type)
from table1
where type like '%Vehicle%'
where type like '%Craft%';

【讨论】:

  • 你好,桌子已经摆好了,我已经给了。基本上我们只知道我们需要找到所有类型的车辆。因此,从表 1 中选择 distinct(type),其中 '%Vehicle%' 之类的类型只会给出三种专利类型的车辆。此外,我只展示了一个类似的示例表。真实表有 50k + 行
  • 然后也许选择 distinct(type), item
  • DISTINCT 不是列上的函数,它是SELECT DISTINCT 的一部分,适用于整个选定行。 (例如,SELECT DISTINCT (c1), c2...SELECT DISTINCT c1, c2... 相同,与SELECT DISTINCT c1, (c2)... 相同。)
【解决方案2】:

嗨,桑贾亚·韦拉科迪,

鉴于数据模型(糟糕的)设计,要找到一种有效的方法来做你想做的事并不容易。 你可以应用这样的东西;

with Category AS
(
SELECT DISTINCT [Type] as label, [Type]
FROM table1 WHERE [Type] like '%Vehicle%' OR [Type] like '%Air Craft%')
, 
Aux as
(
    SELECT 0 as auxToOrder,  label, [type] FROM Category
    UNION
    SELECT 1 as auxToOrder , item, [type] FROM Table1 WHERE [Type] like '%Vehicle%' OR [Type] like '%Air Craft%'

)

select auxToOrder, [type], label 
from Aux
Order by [type], auxToOrder

结果

auxToOrder    type    label
0    Air Craft    Air Craft
1    Air Craft    Helicopter
1    Air Craft    Jet
0    Land Vehicle    Land Vehicle
1    Land Vehicle    Bus
1    Land Vehicle    Car
1    Land Vehicle    Van
0    Vehicle    Vehicle
1    Vehicle    Air Craft
1    Vehicle    Land Vehicle
1    Vehicle    Water Vehicle
0    Water Vehicle    Water Vehicle
1    Water Vehicle    Boat
1    Water Vehicle    Ship

【讨论】:

  • 基本上查询需要做的是选择所有车辆类型的东西,然后将结果集作为搜索条件并再次选择。例如,如果有三种类型的车辆,下一步应该是一个接一个地拿这三个并检查下面是否有任何东西。如果有的话,一一查找,直到树结构结束。
  • 这没有给出想要的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多