SQLITE INNER JOIN错误的结果

时间:2017-05-23 14:53:23

标签: python sqlite

我在python上有以下Sqlite代码:

cur.execute("CREATE TABLE OB (ID INTEGER PRIMARY KEY AUTOINCREMENT , Name TEXT, Item1 TEXT ,item2 TEXT,Item3 TEXT)")
cur.execute("INSERT INTO OB (name,item1,item2,item3) VALUES('Asfoor','a','b','c')")
cur.execute("INSERT INTO OB (name,item1,item2,item3) VALUES('deek','a','x','v')")
cur.execute("INSERT INTO OB (name,item1,item2,item3) VALUES('bata','k','o','p')")
cur.execute("INSERT INTO OB (name,item1,item2,item3) VALUES('wiza','q','w','e')")

con.commit() 
cursor = cur.execute("SELECT DISTINCT a.*,b.* from OB a INNER JOIN OB b on a.item1=b.item1  ")

它应该仅返回带有item1 = item2的记录,因此它应该返回 'Asfoor','a','b','c' 'deek','a','x','v' 其中item1 in ='a' 但相反,我继续得到他们加上: 'bata','k','o','p' 'wiza','q','w','e' 其中item1具有不同的值 我不明白为什么我会继续得到那些额外错误的结果。

它应该像这样,但我没有 https://i.stack.imgur.com/0GQmm.png

完成输出:     ID = 1 NAME = Asfoor Item1 = a Item2 = b Item3 = c ID = 1 NAME = Asfoor Item1 = a Item2 = b Item3 = c ID = 2 NAME = deek Item1 = a Item2 = x Item3 = v ID = 2 NAME = deek Item1 = a Item2 = x Item3 = v ID = 3 NAME = bata Item1 = k Item2 = o Item3 = p ID = 4 NAME = wiza Item1 = q Item2 = w Item3 = e

2 个答案:

答案 0 :(得分:0)

我无法复制您的搜索结果:

import pprint as pp
pp.pprint(cur.execute("""SELECT DISTINCT a.*,b.* 
                           FROM OB a 
                               INNER JOIN OB b 
                               ON a.item1=b.item1;""").fetchall())

[(1, u'Asfoor', u'a', u'b', u'c', 1, u'Asfoor', u'a', u'b', u'c'),
 (1, u'Asfoor', u'a', u'b', u'c', 2, u'deek', u'a', u'x', u'v'),
 (2, u'deek', u'a', u'x', u'v', 1, u'Asfoor', u'a', u'b', u'c'),
 (2, u'deek', u'a', u'x', u'v', 2, u'deek', u'a', u'x', u'v'),
 (3, u'bata', u'k', u'o', u'p', 3, u'bata', u'k', u'o', u'p'),
 (4, u'wiza', u'q', u'w', u'e', 4, u'wiza', u'q', u'w', u'e')]

每次都有匹配a.item1 = b.item1

答案 1 :(得分:-1)

试试这个

highlightCtrl = new OpenLayers.Control.SelectFeature([routesLayer], { hover: true, highlightOnly: true, eventListeners: { featurehighlighted: HighLightRoute, featureunhighlighted: unHighLightRoute } });

它解决了您尝试过的表的别名。您可能需要查看有关SQLite aliasing

的教程或文档