在SQLite中选择介于1到100之间的数字

0g0grzrc  于 2022-11-15  发布在  SQLite
关注(0)|答案(7)|浏览(228)

我知道SQL中的一些解决方案,但在SQlite中找不到任何解决方案。
我只想执行一个SELECT查询,返回一个范围从1到100的数字结果集。

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

更正一下:我实际上根本没有table。(不过,建议使用MySQL中的dual这样的虚拟表作为解决方案)

aij0ehis

aij0ehis1#

谢谢斯格门策!受到你的回答的启发,我继续前进,也找到了this

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

4ioopgfo2#

怎么样

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

kxxlusnw

kxxlusnw3#

在SQLite中生成系列1<=n<=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);
brccelvz

brccelvz4#

我认为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提供了一个很好的理由。

tcomlyy6

tcomlyy65#

SELECT * FROM Numbers limit 1, 100;
hiz5n14c

hiz5n14c6#

如果您的目标是从值介于1和100之间的表中选择实际记录,请使用其他受访者所示的介于两者之间。
如果您的目标是生成一个从1到100的数字序列,而没有一个表作为它的基础,我不相信SQLite有这样的功能。

pdsfdshx

pdsfdshx7#

SELECT * FROM myTable WHERE myNumber BETWEEN 1 AND 100;

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

相关问题