我有12个位置,每个位置都运行自己的数据库。每个数据库都设置完全相同,具有自己的信息。数据库都是SQL2005或2008服务器。我写了一个多表查询,以获取支付销售佣金的信息。此查询适用于12个数据库中的10个,其他2个数据库不返回任何结果。如果我使用2个连接表运行查询(在2个数据库上它不起作用)它可以工作,但是其他表格与“where”语句无关。任何人都可以解释为什么它不适用于2005年的2个服务器数据库,如果它适用于所有其他数据库?我的代码是:
SELECT Customers.sBarcode AS Barcode, Customers.dtCreated AS Created, CustomerUnlimitedTransactions.sCreatedBy AS [Commission pays],
Surcharges.sDescription AS [Unlimited Package], Surcharges.dblCommission AS [Commission Amount], Users.sUserName AS Greeter, @startdate AS Start_Date,
@enddate AS End_Date
FROM CustomerUnlimitedTransactions INNER JOIN
Customers ON CustomerUnlimitedTransactions.lCustomerId = Customers.lCustomerID INNER JOIN
VIPAccountTypes ON CustomerUnlimitedTransactions.lMonthlyAccountTypeId = VIPAccountTypes.lAccountType INNER JOIN
Surcharges ON VIPAccountTypes.lSurchargeId = Surcharges.Surcharge_ID INNER JOIN
Users ON Customers.lGreeterID = Users.ID_User
WHERE (Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102)) AND
(CustomerUnlimitedTransactions.sTransactionType = N'sale') AND (CustomerUnlimitedTransactions.lMonthlyAccountTypeId = '11') OR
(Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102)) AND
(CustomerUnlimitedTransactions.sTransactionType = N'sale') AND (CustomerUnlimitedTransactions.lMonthlyAccountTypeId = '12')
ORDER BY Created
如果我像这样运行查询,我仍然不会返回任何结果:
SELECT Customers.sBarcode AS Barcode, Customers.dtCreated AS Created, CustomerUnlimitedTransactions.sCreatedBy AS [Commission pays],
Surcharges.sDescription AS [Unlimited Package], Surcharges.dblCommission AS [Commission Amount], Users.sUserName AS Greeter, @startdate AS Start_Date,
@enddate AS End_Date
FROM CustomerUnlimitedTransactions INNER JOIN
Customers ON CustomerUnlimitedTransactions.lCustomerId = Customers.lCustomerID INNER JOIN
VIPAccountTypes ON CustomerUnlimitedTransactions.lMonthlyAccountTypeId = VIPAccountTypes.lAccountType INNER JOIN
Surcharges ON VIPAccountTypes.lSurchargeId = Surcharges.Surcharge_ID INNER JOIN
Users ON Customers.lGreeterID = Users.ID_User
WHERE (Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102))
ORDER BY Created
如果我像这样运行查询,我会得到结果:
SELECT Customers.sBarcode AS Barcode, Customers.dtCreated AS Created, CustomerUnlimitedTransactions.sCreatedBy AS [Commission pays],
Surcharges.sDescription AS [Unlimited Package], Surcharges.dblCommission AS [Commission Amount], Users.sUserName AS Greeter, @startdate AS Start_Date,
@enddate AS End_Date
FROM CustomerUnlimitedTransactions INNER JOIN
Customers ON CustomerUnlimitedTransactions.lCustomerId = Customers.lCustomerID
WHERE (Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102) AND CONVERT(DATETIME, @enddate, 102))
ORDER BY Created
没有任何Null与上述任何查询相关联。再次这适用于12个中的10个......是的,数据库完全相同。 (如果我在一年的日期范围内运行此查询,我会得到结果)但是我应该仅显示上周的25个结果,并且我在每个日期都没有显示结果。
答案 0 :(得分:0)
如果数据库实际上是相同的(请参阅Tony Hopkinson的评论),那么请查看您正在进行JOIN
的字段以查看是否存在任何空值。
答案 1 :(得分:0)
提示:IN关键字为WHERE子句
创建奇迹WHERE Customers.dtCreated BETWEEN CONVERT(DATETIME, @startdate, 102)
AND CONVERT(DATETIME, @enddate, 102)
AND CustomerUnlimitedTransactions.sTransactionType = N'sale'
AND CustomerUnlimitedTransactions.lMonthlyAccountTypeId in ('11',12)
对于不影响JOIN中其他表的WHERE子句,我非常怀疑你需要将它们从INNER JOIN转换为LEFT JOIN。
正如所写(提示#2:请使用合理的别名自由)
FROM CustomerUnlimitedTransactions T
INNER JOIN Customers C ON T.lCustomerId = C.lCustomerID
INNER JOIN VIPAccountTypes V ON T.lMonthlyAccountTypeId = V.lAccountType
INNER JOIN Surcharges S ON V.lSurchargeId = S.Surcharge_ID
INNER JOIN Users U ON C.lGreeterID = U.ID_User
如果要在最终结果中显示一行,则客户必须至少有一个涉及收费的VIP帐户类型的交易,并且必须受到欢迎(松散读取联接)。 JOIN和行上的任何空值都会消失。