在sqlite中选择范围(1到100)之间的数字

时间:2010-11-22 13:10:04

标签: sqlite

我知道SQL中的一些解决方案,但是从SQlite找不到任何解决方案。

我只想执行一个select查询,该查询返回一个从1到100的数字结果集。

Numbers
  1
  2
  3
  4
  ......
  5

更正:我根本没有桌子。 (但是,我们鼓励使用像MySQL中的dual这样的虚拟表)

6 个答案:

答案 0 :(得分:24)

谢谢sgmentzer! 受到你答案的启发,我继续前进并找到了this

WITH RECURSIVE
  cnt(x) AS (
     SELECT 1
     UNION ALL
     SELECT x+1 FROM cnt
      LIMIT 100000
  )
SELECT x FROM cnt;

答案 1 :(得分:6)

怎么样

SELECT * FROM myTable WHERE myNumber >= 1 AND myNumber <= 100;

答案 2 :(得分:6)

在SQLite中生成系列1&lt; = n&lt; = 100000的示例子查询。没有创建或使用表格。

select 1+e+d*10+c*100+b*1000+a*10000 as n from
(select 0 as a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as b union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as c union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as d union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as e union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9);

答案 3 :(得分:3)

我不认为SQLite有一个干净的方法,所以你需要使用虚拟表接口。 SQLite为'C'发布了一个,而apsw有一个用于python,我将在下面演示。这是APSW Virtual Table interface的文档。

#!/usr/bin/python
import apsw,tempfile

### Opening/creating database
filename=tempfile.mktemp() #insecure - do not use in production code
connection=apsw.Connection(filename)
cursor=connection.cursor()

# This gets registered with the Connection
class Source:
    def Create(self, db, modulename, dbname, tablename, *args):
        schema="create table foo( dummy integer )"
        return schema,Table()
    Connect=Create

# Represents a table
class Table:
    def __init__(self):
        pass

    def BestIndex(self, constraints, orderbys):
        used = []
        self.constraints = []
        ucount = 0
        for c in constraints:
            if c[1] in (
                         apsw.SQLITE_INDEX_CONSTRAINT_GT, 
                         apsw.SQLITE_INDEX_CONSTRAINT_GE,
                         apsw.SQLITE_INDEX_CONSTRAINT_LT,
                         apsw.SQLITE_INDEX_CONSTRAINT_LE,
                         apsw.SQLITE_INDEX_CONSTRAINT_EQ,
                       ):
                used.append( ucount ) #tell sqlite we want to use this one
                self.constraints.append( c[1] ) #save some for later
            else:
                used.append( None ) #skip anything we don't understand
            ucount += 1
        return ( used,    # used constraints list
                  0,      # index number - no biggie we only support one right now
               )

    def Open(self):
        return Cursor(self)

    def Disconnect(self):
        pass

    Destroy=Disconnect

# Represents a cursor
class Cursor:
    def __init__(self, table):
        self.table=table

    def Filter(self, indexnum, indexname, constraintargs):
        start = 0
        self.end = 4000000000
        #map constraint arguments to start and end of generation
        for tc, ca in zip( self.table.constraints, constraintargs ):
            if tc == apsw.SQLITE_INDEX_CONSTRAINT_EQ:
                start = ca
                self.end = ca
            elif tc == apsw.SQLITE_INDEX_CONSTRAINT_LE:
                if self.end > ca:
                    self.end = ca
            elif tc == apsw.SQLITE_INDEX_CONSTRAINT_LT:
                if self.end >= ca:
                    self.end = ca
            elif tc == apsw.SQLITE_INDEX_CONSTRAINT_GE:
                if start < ca:
                    start = ca
            elif tc == apsw.SQLITE_INDEX_CONSTRAINT_GT:
                if start >= ca:
                    start = ca
        self.pos = start

    def Eof(self):
        return self.pos > self.end

    def Rowid(self):
        return self.pos

    def Next(self):
        self.pos+=1

    def Close(self):
        pass

# Register the module as intsource, you can make a bunch if needed
connection.createmodule("intsource", Source())

# Create virtual table to use intsource
cursor.execute("create virtual table uints using intsource()")

# Do some counting
for i in cursor.execute("SELECT rowid FROM uints WHERE rowid BETWEEN 1 AND 100"):
    print i

这实现了一个名为“intsource”的虚拟表类型,默认情况下从0到4 * 10 ^ 9。它支持通过相等和比较直接过滤,但任何其他约束仍将被sqlite过滤掉。虚拟表是一个非常强大的概念,你可以做很多事情,这可能是它们最简单的用途之一。另外,非常感谢您尝试使用新的虚拟表API。

答案 4 :(得分:0)

如果您的目标是从值为1到100的表中选择实际记录,请使用BETWEEN,如其他受访者所示。

如果您的目标是生成一个从1到100的数字序列而没有基于它的表格,我不相信SQLite有这样的功能。

答案 5 :(得分:-2)

SELECT * FROM myTable WHERE myNumber BETWEEN 1 AND 100;

这比使用2个WHERE子句更有效。