假设我有一个查询
onEvent
我想要第1,第5,第100,第250的结果,等等。
a.action-button {
display: flex;
flex: 1 1 0;
justify-content: space-between;
border: 4px solid thistle;
}
a.action-button>span {
display: flex;
flex: 1 1 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
给您第五。而且我可以运行类似的查询N次。但是,如何仅通过一个查询就能更优雅地做到这一点?
像这样的东西(不起作用)会很棒!
SELECT *
FROM foo
ORDER BY id
答案 0 :(得分:4)
一种方法是ROW_NUMBER
(MySQL 8.0):
SELECT *
FROM (SELECT *, ROW_NUMBER() OVER(ORDER BY id) AS rn FROM foo) sub
WHERE rn IN (1, 5, 100, 250)
ORDER BY rn;
答案 1 :(得分:1)
您可以使用MySQL> = 5.x.x
的变量来实现SELECT z.*
FROM (
SELECT *, @row_number:=@row_number+1 AS row_number
FROM foo, (SELECT @row_number:=0) AS t
ORDER BY pkey
) AS z
WHERE row_number IN (1, 5, 100, 250);
此处将变量声明为表t
。 @row_number
每行将增加1,然后可以使用WHERE
子句进行过滤。