该查询:SELECT sc.ContactID , c.Price , p.ParkID FROM tblc c JOIN tblsc ON c.ID= sc.ID LEFT JOIN tblp p ON sc.ID= p.ID WHERE (stuff = stuff) AND (stuff = stuff)返回此:ContactID LeadSalePrice ParkID------------------------------------- 1 50 NULL 2 60 22现在,我正在尝试为AND设置另一个ParkID子句。但是,我只想在AND不为空的行上执行此ParkID。因此,如果在ParkID为NULL的行上,我们希望始终保留这些行。在ParkID不为NULL的行上,仅当ParkID与参数匹配时才保留这些行。如果@ParkID = 22,则返回两行。如果@ParkID 22,则仅返回第一行。如果@ParkID = NULL,则返回两行。我已经试过了:SELECT sc.ContactID , c.Price , p.ParkIDFROM tblc c JOIN tblsc ON c.ID= sc.ID LEFT JOIN tblp p ON sc.ID= p.ID WHERE (stuff = stuff) AND (stuff = stuff) AND p.ParkID = CASE WHEN p.ParkID IS NULL THEN NULL ELSE @ParkID END这不起作用,因为与null比较的正确方法不是:= NULL它的IS NULL出于相同的原因(我假设),这也不起作用:AND p.ParkID = Isnull(p.ParkID, @ParkID)我该怎么做呢? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 只需检查p.ParkID是NULL还是与@ParkID匹配:WHERE p.ParkID IS NULL OR p.ParkID = @ParkID关于sql - 在WHERE子句中使用IsNull(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42164372/ (adsbygoogle = window.adsbygoogle || []).push({});
10-10 01:55