【问题标题】:Nested result sets as with dynamic names与动态名称一样的嵌套结果集
【发布时间】:2016-03-09 20:51:00
【问题描述】:

我正在尝试连接两个引用表的结果以获取引用不同表名的行值,哪些行可以通过它们的uuid 选择。

我的表格如下所示:

表格条目 表地图 餐桌猫 餐桌狗

nrrefInt  id  name                 ma​​pRef 品种  ma​​pRef    breed    
 1   123  123'狗'    456    '孟加拉'    123    '牧羊犬' 
 2  456   456'猫'    888     'birma'                      999       '贵宾犬'   
 3  789   789'狗'
 4  123  

refInt 的条目引用映射。 map 的名称是对表的引用,此外还有字段 id,该字段 id 也应用于表 cat/dogs(动态表加载)。

// subset 1: list of numbers that needs to be loaded from entry table (1-4)
SELECT DISTINCT refInt FROM entry WHERE nr in (1,2,3,4)

// subset 2: get all names from map that have the same id like refInt from subset1
SELECT name FROM map WHERE id in subset1

// main query: load all rows from table with the given name
//             from map table that have the same mapRef value on it
SELECT * FROM (subset2.names) WHERE mapRef IN (subset2.ids)

结果应该是行:

1) 456 bengal
2) 123 sheepdog

我还为它做了一个SQLFiddle。

有没有办法将它组合到一个查询中?

【问题讨论】:

  • 你用 sql-server 标记了这个,但你的小提琴是 mysql 并且使用的是 sql-server 不可用的数据类型。是哪一个?
  • 您的第一个SELECT 中有错字吗?实际上应该是:SELECT DISTINCT refInt FROM entry WHERE nr in (1,2,3,4) ???
  • 为什么架构不是normalized?地图甚至不应该存在。
  • 根据您对@ErikPhilips 回答的评论,您将需要使用动态 sql 来处理此问题。
  • @TabAlleman 或标准化数据库..

标签: sql sql-server


【解决方案1】:

它看起来像:

SqlFiddle

select 
  sub.nr, 
  sub.breed
from (
    select e.nr, e.refInt, 
      case 
        when c.breed is not null then c.breed
        when d.breed is not null then d.breed
        else null
      end as breed
    from (
       select e.nr, e.refInt, m.name
       from entry e
       inner join map m on e.refInt = m.id
    ) e
    left join cats c on e.refInt = c.mapRef and e.name = 'cats'
    left join dogs d on e.refInt = d.mapref and e.name = 'dogs'
) sub
where sub.breed is not null

这在性能上会很差。

现在 IMO 正确的架构是:

table entry
nr refint
1  123
2  456
3  789
4  124 (duplicate?)

table breed
mapRef breed    species
123    sheepdog 1
999    poodle   1
456    bengal   2 
888    birma    2

table species
id  species
1   dogs
2   cats

这是归一化的,性能非常好。

【讨论】:

  • 谢谢,但表格(猫/狗)的名称不是静态的 - 假设有 100 个表格,并且名称列链接到正确的表格。
  • 是的,这就是问题所在,你没有一个好的架构。存储 Tablename 以在其中查找值不是最佳做法,很难做到并且性能可能最差。
  • @kerosene,如果可能的话,按照 Erik 的建议更新架构。如果没有,您可以考虑创建一个视图,将您的所有狗/猫表与其他列表名联合起来,您可以以正常方式加入它。但是,那样的话性能会很差
  • 好的,谢谢大家——我认为如果你不知道场景/用例,你不应该判断。我无法更改架构。我的替代方法是从表映射中获取所有表名并触发第二个 sql 查询。
  • 评判是件好事。当我去一家公司工作时,我要求他们看一些代码。如果那家公司正在编写糟糕的代码..假设他们希望我编写这样的代码,我为什么要为他们工作......
【解决方案2】:

请注意以下查询如何完全实现所需的结果集,并充分展示表 cats 和 dogs 是如何真正只是一个 partitions单个实体动物。应该重新设计模式以反映这种新的理解。这个查询也很有效,因为包含测试 id 被推到了最里面的 CTE 的深度,在读取实际表行的级别,而不依赖于引擎来发现这种潜在的优化(这可能对 UNION 有问题)。

with
cats2 as (
  select species='cat', mapref, breed
  from cats animals
  join entry on entry.refint = animals.mapref
  where entry.nr  in (1,2,3,4)
),
dogs2 as (
  select species='dog', mapref, breed
  from dogs animals
  join entry on entry.refint = animals.mapref
  where entry.nr  in (1,2,3,4)
),
animals as (
  select species, mapref, breed from cats2
  union all
  select species, mapref, breed from dogs2
)
select species, mapref, breed 
from animals
group by species, mapref, breed 

这个测试脚本:

declare @entry table (nr int, refint int );
declare @map table (id int, name varchar(20) );
declare @cats table (mapRef int, breed varchar(20));
declare @dogs table (mapRef int, breed varchar(20));

insert @entry(nr,refint) values
     (1,123)
    ,(2,456)
    ,(3,789)
    ,(4,123);
insert @map(id,name) values
     (123,'dogs')
    ,(456,'cats')
    ,(789,'dogs');

insert @cats(mapRef,breed) values
    (456,'bengal'),(888,'burma');

insert @dogs(mapRef,breed) values
    (123,'sheepdog'), (999,'poodle');


with
cats2 as (
  select species='cat', mapref, breed
  from @cats animals
  join @entry entry on entry.refint = animals.mapref
  where entry.nr  in (1,2,3,4)
),
dogs2 as (
  select species='dog', mapref, breed
  from @dogs animals
  join @entry entry on entry.refint = animals.mapref
  where entry.nr  in (1,2,3,4)
),
animals as (
  select species, mapref, breed from cats2
  union all
  select species, mapref, breed from dogs2
)
select species, mapref, breed 
from animals
group by species, mapref, breed 

满足要求:

species mapref      breed
------- ----------- --------------------
cat     456         bengal
dog     123         sheepdog

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 1970-01-01
    • 2016-04-30
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    相关资源
    最近更新 更多