代码之家  ›  专栏  ›  技术社区  ›  Dimitri Tcaciuc

SQLite在id上自我连接时出现错误?

  •  3
  • Dimitri Tcaciuc  · 技术社区  · 17 年前

    我试图根据行的id生成行的成对组合。SQLite的版本是3.5.9。表格内容如下:

    id|name|val  
    1|A|20
    2|B|21
    3|C|22
    

    表模式为:

    CREATE TABLE mytable (
        id INTEGER NOT NULL, 
        name VARCHAR, 
        val INTEGER, 
        PRIMARY KEY (id)
    );
    

    然后是id上的自连接:

    sqlite> select t1.id, t2.id from mytable as t1, mytable as t2 where t2.id > t1.id;
    id|id
    2|2
    2|3
    3|3
    

    这显然不是我想要的。现在,改变t2和t1的顺序会产生正确的结果:

    sqlite> select t1.id, t2.id from mytable as t2, mytable as t1 where t2.id > t1.id;
    id|id
    1|2
    1|3
    2|3
    

    现在,在另一个实验中,我尝试在除行id之外的数字列上进行组合。另一方面,这在两种情况下都能得到正确的结果。

    我希望有人能了解这里发生了什么。据我所知,这要么是SQLite中的一个bug,要么是我不知道的SQL的一些微妙方面。

    谢谢,

    2 回复  |  直到 17 年前
        1
  •  4
  •   tommym    17 年前

    这似乎是SQLite中的一个错误——正如你所怀疑的那样,你发布的第一个结果是错误的。我在我的工作站上的PG8.3和sqlite3.6.4上测试了它,但无法再现。在所有情况下都得到了正确的结果。可能链接到您的sqlite版本;尝试升级。

        2
  •  0
  •   converter42    17 年前
    SQLite version 3.6.2
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> create table mytable (
       ...> id integer not null,
       ...> name varchar,
       ...> val integer,
       ...> primary key (id)
       ...> );
    sqlite> insert into mytable values(null,'A',20);
    sqlite> insert into mytable values(null,'B',21);
    sqlite> insert into mytable values(null,'C',22);
    sqlite> select t1.id, t2.id from mytable as t1, mytable as t2 where t2.id > t1.id;
    1|2
    1|3
    2|3
    
    推荐文章