【问题标题】:Finding rows that do not have particular child rows查找没有特定子行的行
【发布时间】:2021-03-08 19:29:18
【问题描述】:

我有两个与图像有关的表格:

ImageInfo
---------
Id
Name
Photographer

Images
------
Id
ImageInfo_Id
Quality
URL

ImagesImageInfo 具有多对一的外键关系,并且对于给定的 ImageInfo 条目,可以保持各种质量——600、800 和其他像素分辨率。

我所追求的是所有在图像表中具有 600 像素质量条目的 ImageInfo ID。我怎样才能最好地做到这一点?我正在考虑左外连接或者where not exists 声明,但想在走这条路之前征求意见。

【问题讨论】:

    标签: sql


    【解决方案1】:

    听起来像not exists

    select ii.*
    from imageinfo ii
    where not exists (select 1
                      from images i
                      where i.ImageInfo_Id = ii.id and
                            i.quality = '600 pixel' -- or whatever
                     );
    

    您也可以将其表述为left join,但not exists 几乎是您问题的直接翻译。

    【讨论】:

      猜你喜欢
      • 2021-03-15
      • 2018-03-06
      • 2011-05-12
      • 2021-08-30
      • 1970-01-01
      • 2014-02-26
      • 2023-01-31
      • 2014-10-06
      • 1970-01-01
      相关资源
      最近更新 更多