繁体   English   中英

SQL查询中的WHERE子句

[英]WHERE clause in an SQL query

我认为此查询正在发生的事情是,如果与该产品关联的GenericAttribute表中没有记录,则该产品不会显示。 请参见WHERE子句中的以下行:“ AND GenericAttribute.KeyGroup ='Product'”

如果GenericAttribute表中没有关联的记录,有没有一种方法可以改写,以便忽略WHERE的那一部分?

另外,看看我的ORDER BY子句,如果产品表中的记录在Pvl_AdDates表中没有关联的记录,还会显示吗?

谢谢!

SELECT DISTINCT Product_Category_Mapping.CategoryId, Product.Id, Product.Name, Product.ShortDescription, Pvl_AdDates.Caption, Pvl_AdDates.EventDateTime, convert(varchar(25), Pvl_AdDates.EventDateTime, 120) AS TheDate, Pvl_AdDates.DisplayOrder, Pvl_Urls.URL, [Address].FirstName, [Address].LastName, [Address].Email, [Address].Company, [Address].City, [Address].Address1, [Address].Address2, [Address].ZipPostalCode, [Address].PhoneNumber 
FROM [Address] 
RIGHT JOIN (GenericAttribute  
RIGHT JOIN (Pvl_Urls RIGHT JOIN (Pvl_AdDates 
RIGHT JOIN (Product_Category_Mapping 
LEFT JOIN Product 
ON Product_Category_Mapping.ProductId = Product.Id) 
ON Pvl_AdDates.ProductId = Product.Id) 
ON Pvl_Urls.ProductId = Product.Id) 
ON GenericAttribute.EntityId = Product.Id) 
ON Address.Id = convert(int, GenericAttribute.Value) 
WHERE 
Product_Category_Mapping.CategoryId=12 
AND GenericAttribute.KeyGroup = 'Product'
AND Product.Published=1 
AND Product.Deleted=0 
AND Product.AvailableStartDateTimeUtc <= getdate() 
AND Product.AvailableEndDateTimeUtc >= getdate() 
ORDER BY 
Pvl_AdDates.EventDateTime DESC, 
Product.Id, 
Pvl_AdDates.DisplayOrder

我强烈建议您不要混合使用left joinright join 我已经写了许多SQL查询,无法想到有必要的情况。

实际上,只是坚持left join

如果要使用所有产品(或至少所有未通过where子句过滤掉的产品),请从products表开始,然后从那里开始:

FROM Products p LEFT JOIN
     Product_Category_Mapping pcm
     ON pcm.ProductId = p.Id LEFT JOIN
     Pvl_AdDates ad
     ON ad.ProductId = p.id LEFT JOIN
     Pvl_Urls u
     ON u.ProductId = p.id LEFT JOIN
     GenericAttribute ga
     ON ga.EntityId = p.id LEFT JOIN
     Address a
     ON a.Id = convert(int, ga.Value) 

请注意,我添加了表别名。 这些使查询更易于编写和阅读。

我要加个警告。 看起来您正在组合不同维度的数据。 您可能会获得每个维度的维度属性的笛卡尔积。 也许这就是您想要的,或者WHERE子句负责其他行。

是的,在外部联接的条件(而非where子句)中,在外部联接的外侧的表上放置约束(约束)。 直到对外部联接进行评估之后,才对条件进行评估,因此在外部表中没有记录的地方,谓词将为false,并且整个行将被消除,从而消除外部性。 联接中的条件在联接过程中进行评估,然后再从内侧添加行,因此结果集仍将包括它们。

二,格式化格式化,格式化! 坚持连接的一个方向(左侧更容易),并为表名使用别名!

SELECT DISTINCT m.CategoryId, p.Id, 
    p.Name, p.ShortDescription, d.Caption, d.EventDateTime, 
    convert(varchar(25), d.EventDateTime, 120) TheDate, 
    d.DisplayOrder, u.URL, a.FirstName, a.LastName, 
    a.Email, a.Company, a.City, a.Address1, a.Address2, 
    a.ZipPostalCode, a.PhoneNumber 
FROM Product_Category_Mapping m
    left join Product p on p.Id = m.ProductId 
        and p.Published=1 
        and p.Deleted=0 
        and p.AvailableStartDateTimeUtc <= getdate() 
        and p.AvailableEndDateTimeUtc >= getdate() 
    left join Pvl_AdDates d ON d.ProductId = p.Id 
    left join Pvl_Urls u ON u.ProductId = p.Id
    left join GenericAttribute g ON g.EntityId = p.Id
        and g.KeyGroup = 'Product'
    left join [Address] a ON a.Id = convert(int, g.Value) 
WHERE m.CategoryId=12                  
ORDER BY d.EventDateTime DESC, p.Id, d.DisplayOrder

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM