Let' say we have this dataset:
import sqlite3
db = sqlite3.connect(':memory:')
c = db.cursor()
c.execute('CREATE TABLE mytable (id integer, description text)')
c.execute('INSERT INTO mytable VALUES (2, "abc")')
c.execute('INSERT INTO mytable VALUES (5, "def")')
c.execute('INSERT INTO mytable VALUES (18, "geh")')
c.execute('INSERT INTO mytable VALUES (19, "ijk")')
c.execute('INSERT INTO mytable VALUES (27, "lmn")')
How to find pairs / couples of rows for which their ID is distant of at most 3? i.e. it should return the rows (2,5) and (18, 19), and maybe also (5,2), (19,18) (but these two last ones are not needed).
I tried:
c.execute('SELECT id as id1 FROM mytable, SELECT id as id2 FROM mytable WHERE abs(id1 - id2) <= 3')
but it does not seem to work:
sqlite3.OperationalError: near "SELECT": syntax error