我在mysql.connector中发现了一些非常奇怪的行为。具有三个内部联接的SQL查询返回一个空集,但是两个内部联接返回正确的结果。
query = 'select * from `ont_system` inner join compounds_crude ON(compounds_crude.id=compound_id) inner join crude2chebi on(compounds_crude.id=crude_id) inner join tc2acc on (ont_system.tcid=tc2acc.tcid) where crude2chebi.approved=1 limit 10;'
此代码将返回一个空集。但是,如果删除最后一个INNER JOIN,则将在python中得到结果。修改后的查询为:
query = 'select * from `ont_system` inner join compounds_crude ON(compounds_crude.id=compound_id) inner join crude2chebi on(compounds_crude.id=crude_id) inner join tc2acc on (ont_system.tcid=tc2acc.tcid) where crude2chebi.approved=1 limit 10;'
但是,两个查询都在MySQL Shell中工作。最后一个破坏python的内部联接是什么? 这是我的python代码:
import cgitb
import cgi
import mysql.connector
import collections
cnx = mysql.connector.connect(user='x', password='x',
host='127.0.0.1',
database='x')
cursor = cnx.cursor()
Assignment = collections.namedtuple('Assignment',['chebi_id','tcid','acc'])
def loadItems():
query = 'select * from `ont_system` inner join compounds_crude ON(compounds_crude.id=compound_id) inner join crude2chebi on(compounds_crude.id=crude_id) inner join tc2acc on (ont_system.tcid=tc2acc.tcid) where crude2chebi.approved=1 limit 10;'
# This query will make some redundancies, since there may be several acc's per TCID
cursor.execute(query)
print cursor.fetchall()
if __name__=='__main__':
loadItems()