我正在从原件生成图像并将它们存储在表格中。我想要一个检查间隙的查询。
图像尺寸保存在第3张表格中,每张原稿应为类型表格中的每条记录生成1张图像。
到目前为止我所得到的是什么:
SELECT oi.OriginalImageID, it.ImageTypeID
FROM dbo.OriginalImages AS oi
CROSS JOIN
dbo.ImageType AS it
LEFT OUTER JOIN
(
SELECT oi2.OriginalImageID, it2.ImageTypeID
FROM dbo.OriginalImages AS oi2
INNER JOIN
dbo.GeneratedImages AS gi2 ON gi2.OriginalImageID = oi2.OriginalImageID
INNER JOIN
dbo.ImageType AS it2 ON it2.ImageTypeID = gi2.ImageTypeID
) AS sub ON sub.OriginalImageID = oi.OriginalImageID
AND sub.ImageTypeID = it.ImageTypeID
WHERE (sub.OriginalImageID IS NULL)
哪个有效,但看起来很难看。我想知道是否有一种更优雅的方式来做它。
表格基本上如下:
OriginalImages
OriginalImageID (PK)
Image
GeneratedImages
OriginalImageID (FK)
ImageTypeID (FK)
Image
ImageType
ImageTypeID (PK)
Description
答案 0 :(得分:3)
您应该能够将现有查询简化为:
SELECT oi.OriginalImageID, it.ImageTypeID
FROM dbo.OriginalImages AS oi
CROSS JOIN
dbo.ImageType AS it
LEFT OUTER JOIN
dbo.GeneratedImages AS gi2
ON gi2.OriginalImageID = oi.OriginalImageID AND
gi2.ImageTypeID = it.ImageTypeID
WHERE gi2.OriginalImageID IS NULL
虽然我个人会使用NOT EXISTS:
SELECT oi.OriginalImageID, it.ImageTypeID
FROM dbo.OriginalImages AS oi
CROSS JOIN
dbo.ImageType AS it
WHERE NOT EXISTS
(SELECT NULL FROM dbo.GeneratedImages AS gi2
WHERE gi2.OriginalImageID = oi.OriginalImageID AND
gi2.ImageTypeID = it.ImageTypeID)
(编辑后评论。)