SQL查询中的WHERE子句

SQL查询中的WHERE子句,sql,sql-server,Sql,Sql Server,我认为这个查询的结果是,如果GenericAttribute表中没有与产品关联的记录,那么该产品就不会显示。请参见WHERE子句中的下一行:“AND GenericAttribute.KeyGroup='Product'” 若GenericAttribute表中并没有关联的记录,是否有方法重写WHERE以忽略该部分 另外,查看我的ORDERBY子句,如果在Pvl_AdDates表中没有关联的记录,product表中的记录还会显示吗 谢谢 SELECT DISTINCT Product_Categ

我认为这个查询的结果是,如果GenericAttribute表中没有与产品关联的记录,那么该产品就不会显示。请参见WHERE子句中的下一行:“AND GenericAttribute.KeyGroup='Product'”

若GenericAttribute表中并没有关联的记录,是否有方法重写WHERE以忽略该部分

另外,查看我的ORDERBY子句,如果在Pvl_AdDates表中没有关联的记录,product表中的记录还会显示吗

谢谢

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
选择DISTINCT Product\u Category\u Mapping.CategoryId、Product.Id、Product.Name、Product.ShortDescription、Pvl\u AdDates.Caption、Pvl\u AdDates.EventDateTime、convert(varchar(25)、Pvl\u AdDates.EventDateTime、120)作为日期、Pvl\u AdDates.DisplayOrder、Pvl\u URL.URL、[Address].FirstName、[Address].LastName、[Address].Email、[Address].Company].City,[地址].Address1,[地址].Address2,[地址].ZipPostalCode,[地址].PhoneNumber
发件人[地址]
右连接(GenericAttribute)
右连接(Pvl_URL)右连接(Pvl_地址
右连接(产品类别映射)
左连接乘积
在Product_Category_Mapping.ProductId=Product.Id上)
在Pvl上(AdDates.ProductId=Product.Id)
在Pvl上(url.ProductId=Product.Id)
在GenericAttribute.EntityId=Product.Id上)
ON Address.Id=convert(int,GenericAttribute.Value)
哪里
产品类别映射。类别ID=12
和GenericAttribute.KeyGroup='Product'
和Product.Published=1
和Product.Deleted=0
和Product.AvailableStartDateTimeUtc=getdate()
订购人
Pvl_AdDates.EventDateTime DESC,
Product.Id,
Pvl_AdDates.DisplayOrder

我强烈建议您不要将
左加入
右加入
混为一谈。我写过很多SQL查询,想不出有什么必要这样做

事实上,只要坚持左连接即可

如果您想要所有产品(或至少所有未被
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
子句负责处理额外的行。

我强烈建议您不要混合
左连接
右连接
。我写过很多SQL查询,想不出有什么必要这样做

事实上,只要坚持左连接即可

如果您想要所有产品(或至少所有未被
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
子句负责处理额外的行。

是将约束(限制)放在外部联接的on条件中外部联接外侧的表上,而不是放在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
选择不同的m.CategoryId、p.Id、,
p、 姓名,p.ShortDescription,d.Caption,d.EventDateTime,
转换(varchar(25),d.EventDateTime,120)日期,
d、 DisplayOrder、u.URL、a.FirstName、a.LastName、,
a、 电子邮件,a.公司,a.城市,a.地址1,a.地址2,
a、 ZipPostalCode,a.PhoneNumber
来自产品类别映射
在p.Id=m.ProductId上左连接产品p
p.Published=1
p.Deleted=0
p.AvailableStartDateTimeUtc=getdate()
左连接Pvl_在d.ProductId=p.Id上添加d
在u.ProductId=p.Id上左连接Pvl_URL u
g.EntityId=p.Id上的左连接GenericAttribute g
和g.KeyGroup='Product'
a上的左连接[Address]a。Id=转换(int,g.Value)
其中m.CategoryId=12
按d.EventDateTime描述、p.Id、d.DisplayOrder排序
是将约束(限制)放在外部联接的on条件中的外部联接外侧的表上,而不是放在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
选择不同的m.CategoryId、p.Id、,
p、 姓名,p.ShortDescription,d.Caption,d.EventDateTime,
转换(varchar(25),d.EventDateTime,120)日期,
d、 DisplayOrder、u.URL、a.FirstName、a.LastName、,
a、 电子邮件,a.公司,a.城市,a.地址1,a.地址2,
a、 ZipPostalCode,a.PhoneNumber
来自产品类别映射
在p.Id=m.ProductId上左连接产品p
p.Published=1
p.Deleted=0
p.AvailableStartDateTimeUtc=getdate()
左连接Pvl_在d.ProductId=p.Id上添加d
在u.ProductId=p.Id上左连接Pvl_URL u
左边