简体   繁体   English

内部联接在SQL Server中给出行号不希望的结果

[英]Inner join gives undesired result with row number in sql server

I've tried to do a pagination on data in using ROW_NUMBER() 我尝试使用ROW_NUMBER()对数据进行分页
Here is my query: 这是我的查询:

SELECT * FROM
    (SELECT ROW_NUMBER() OVER (ORDER BY OrderID) AS Row,* FROM SpecificOrders) 
    AS EMP
        inner join Users as c on EMP.UserID = c.UserID
        inner join Users as u on EMP.CreatedBy = u.UserID       
        inner join SpecificOrderPayment as p on EMP.OrderID= p.OrderID

    WHERE Row BETWEEN 0 AND 10 

When I execute this query, I get output like following with : 当我执行此查询时,我得到如下输出:

Row | OrderID | UserID |
1   |         |        |
5   |         |        |
6   |         |        |
7   |         |        |
8   |         |        |
9   |         |        |
10  |         |        |

If I remove this WHERE Row BETWEEN 0 AND 10 condition then it'll gives me all records 如果我删除此“ WHERE Row BETWEEN 0 AND 10条件,它将获得所有记录

Here my question is why I get only 7 rows and why here 2,3 and 4 is missing in the row column. 在这里,我的问题是为什么我只有7行,为什么行列中缺少2,3和4。

Moreover, If i remove 3rd join query ( SpecificOrderPayment )then it will give me proper result. 而且,如果我删除了第3个联接查询( SpecificOrderPayment ),那么它将给我适当的结果。

you've got OrderID that are null or blanks in SpecificOrders and they are sorting to the top - the approach isn't wrong otherwise, although there are other ways of doing it such as TOP 10..etc 您获得的OrderID为null或在SpecificOrders中为空白,并且它们排在最前面-否则方法没有错,尽管还有其他方法可以做到,例如TOP 10..etc

SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY OrderID) AS Row,* FROM SpecificOrders 
                                                  WHERE RTRIM(COALESCE(OrderID, '')) <> '') 
AS EMP
    inner join Users as c on EMP.UserID = c.UserID
    inner join Users as u on EMP.CreatedBy = u.UserID       
    inner join SpecificOrderPayment as p on EMP.OrderID= p.OrderID

WHERE Row BETWEEN 0 AND 10 

The problem is that you are numbering the rows of SpecificOrders and not of final result. 问题是您要对SpecificOrders的行进行编号,而不是最终结果的行。

In your case you only have one row per order, so using left joins should solve the issue 在您的情况下,每个订单只有一行,因此使用左联接应该可以解决问题

But, if the inner query could return multiple rows for each OrderID you will see the same row number many times 但是,如果内部查询可以为每个OrderID返回多行,则您将多次看到同一行号

And if the join (inner) will filter some row you will not get that row number in result. 而且,如果联接(内部)将过滤某些行,则结果中将不会获得该行号。

You have simply to separate the query for data extraction from the query for pagination, 您只需将用于数据提取的查询与用于分页的查询分开,

Try this: 尝试这个:

SELECT * 
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY EMP.OrderID) AS Row, EMP.*
    FROM SpecificOrders AS EMP
    left join Users as c on EMP.UserID = c.UserID
    left join Users as u on EMP.CreatedBy = u.UserID       
    left join SpecificOrderPayment as p on EMP.OrderID= p.OrderID
) D
WHERE [Row] BETWEEN 0 AND 10 

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

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