2008-11-20 27 views
3

我試圖根據它們的id生成行的成對組合。 SQLite版本是3.5.9。該表內容如下:SQLite自加入ID的錯誤?

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) 
); 

再有就是對IDS自聯接:

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 

這顯然不是我想要的。現在,改變T1和T2的順序產生正確的結果:

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中的一個錯誤,就是SQL的一些微妙方面,我不知道。

感謝,

回答

4

似乎是在SQLite的錯誤 - 您發佈的第一個結果是,當你懷疑的,是錯誤的。我已經在我的工作站上對PG8.3和sqlite3.6.4進行了測試,無法重現。在所有情況下都有正確的結果。可能會鏈接到您的sqlite版本;嘗試升級。

+0

通過python確認sqlite 3.3.14中第一個查詢的正確結果。 – 2008-11-20 02:13:52

0
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