SQL查询 - 不返回正确的数据

时间:2011-12-15 16:08:00

标签: mysql sql sql-server-2008

在我的查询中,我需要为搜索返回不同的字段。但是,即使没有,txt_note字段也会返回信息。

SELECT DISTINCT T_ORDER_DETAIL.TXT_ORDER_NUMBER, 
                T_ORDER_DETAIL.CUSTOMER_PRODUCT_ID, 
                T_VENDOR.VENDOR_ID, 
                T_CUSTOMER_ACCOUNT.CUSTOMER_ID, 
                T_EMPLOYEE.TXT_EMAIL, 
                T_ORDER_ASSIGNMENT.TXT_NOTE 
FROM   T_VENDOR_EMPLOYEE 
       INNER JOIN T_VENDOR WITH(nolock) 
               ON T_VENDOR_EMPLOYEE.VENDOR_ID = T_VENDOR.VENDOR_ID 
       INNER JOIN T_CUSTOMER_ACCOUNT WITH(nolock) 
                  INNER JOIN T_ORDER_DETAIL WITH(nolock) 
                             INNER JOIN T_CUSTOMER_PRODUCT WITH(nolock) 
                                     ON T_ORDER_DETAIL.CUSTOMER_PRODUCT_ID = 
                                        T_CUSTOMER_PRODUCT.CUSTOMER_PRODUCT_ID 
                          ON T_CUSTOMER_ACCOUNT.CUSTOMER_ACCOUNT_ID = 
                             T_CUSTOMER_PRODUCT.CUSTOMER_ACCOUNT_ID 
               ON T_VENDOR_EMPLOYEE.VENDOR_EMPLOYEE_ID = 
                  T_ORDER_DETAIL.VENDOR_EMPLOYEE_ID 
       INNER JOIN T_EMPLOYEE WITH(nolock) 
               ON T_VENDOR_EMPLOYEE.EMPLOYEE_ID = T_EMPLOYEE.EMPLOYEE_ID 
       INNER JOIN T_ORDER_DETAIL_REJECTION_REASON WITH(nolock) 
               ON T_ORDER_DETAIL.ORDER_DETAIL_ID = 
                  T_ORDER_DETAIL_REJECTION_REASON.ORDER_DETAIL_ID 
       INNER JOIN T_ORDER_ASSIGNMENT WITH(nolock) 
               ON T_VENDOR_EMPLOYEE.VENDOR_EMPLOYEE_ID = 
                  T_ORDER_ASSIGNMENT.VENDOR_EMPLOYEE_ID 
WHERE  T_ORDER_ASSIGNMENT.INT_ACCEPTED = 4 
       AND T_ORDER_ASSIGNMENT.TXT_NOTE <> '' 

即使它返回信息,当您查看该字段为空白的实际顺序时。

txt_Order_Number    Customer_Product_ID vendor_ID   customer_id txt_email   txt_Note
260247648   555 134242  650 jason@propertysmart.us  Out of office
260498186   2783    134242  429 jason@propertysmart.us  Out of office
261108453b  1485    134242  206 jason@propertysmart.us  Out of office
261240252b  4429    134242  206 jason@propertysmart.us  Out of office
270158583b  4429    134242  206 jason@propertysmart.us  Out of office
270877727   3190    134242  724 jason@propertysmart.us  Out of office
271239690b  4817    134242  486 jason@propertysmart.us  Out of office
280162866b  5378    134242  721 jason@propertysmart.us  Out of office
280968017b  1485    134242  206 jason@propertysmart.us  Out of office
290702640   9361    134242  120 jason@propertysmart.us  Out of office
291294922-50    9972    134242  977 jason@propertysmart.us  Out of office
300453305-25    3648    134242  206 jason@propertysmart.us  Out of office
301297011-50    1922    134242  206 jason@propertysmart.us  Out of office

其中只有一个txt_not实际上不在其中,其余的都是空白的。

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:1)

您的t_Order_Assignment表列是否有Order_Detail_ID?如果是,请在上次连接中将其添加到ON条件

答案 1 :(得分:0)

我会尝试简化查询,从尽可能少的内部联接开始。即:

SELECT * 
FROM t_Vendor_Employee 
INNER JOIN t_Order_Assignment ON t_Vendor_Employee.vendor_employee_id = t_Order_Assignment.vendor_employee_id 
WHERE t_Order_Assignment.int_Accepted = 4 and t_Order_Assignment.txt_Note <> '' 

如果这给出了预期结果,请添加更多内部联接,直到出现错误为止 - 然后查看数据并查看是否可以确定原因。


另外,您是否知道使用WITH(NOLOCK)可能会导致错过已提交的行?

http://blogs.msdn.com/sqlcat/archive/2007/02/01/previously-committed-rows-might-be-missed-if-nolock-hint-is-used.aspx