SQL:如何从表中选择第一,第三,第十一和第n行? [英] SQL: How to select 1st, 3rd, 11th and nth row from a table?
本文介绍了SQL:如何从表中选择第一,第三,第十一和第n行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从表中选择第一,第三,第十一和第n行?
How to select 1st, 3rd, 11th and nth row from a table?
推荐答案
如果为表定义的主键是基于整数的数据类型-例如MySQL和SQLite都具有auto_increment,则可以使用:
If there is a primary key defined for the table that is an integer based data type--both MySQL and SQLite have auto_increment for example--then you can use:
SELECT t.*
FROM TABLE t
WHERE t.id IN (1,3, 11)
...其中id
是auto_increment列.
...where id
is the auto_increment column.
几乎没有什么细节可做,但是MySQL和SQLite没有分析查询支持,这使得查询相当复杂:
There's very little detail to go on, but MySQL and SQLite do not have analytic query support, making queries rather complicated:
SELECT y.*
FROM (SELECT t.*,
(SELECT COUNT(*)
FROM TABLE x
WHERE x.col <= t.col) AS rank
FROM TABLE t) y
WHERE y.rank IN (1, 3, 11)
这篇关于SQL:如何从表中选择第一,第三,第十一和第n行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文