0

我在两个不同的表中有两列。
PCTNo有这个值:US2002014866
pct_publishing_data有这个值:JP1014866
我想得到最后 5 个值PCTNo和最后 5 个值,pct_publishing_data如果 5 位pct_publishing_dataPCTNo和前 2 个值相同,那么它会显示出来。在这种情况下它不会出现

这是我一直在实现的代码。

import MySQLdb

db = MySQLdb.connect(host = "localhost", user = "root", passwd="", db="fak")
cursor = db.cursor()

#Execute SQL Statement:
cursor.execute("SELECT auip_wipo_sample.PCTNo FROM auip_wipo_sample INNER JOIN us_pat_2005_to_2012 ON RIGHT(auip_wipo_sample.PCTNo,5) = RIGHT(us_pat_2005_to_2012.pct_publishing_data,5) AND LEFT(auip_wipo_sample.PCTNo,2) = LEFT(us_pat_2005_to_2012.pct_publishing_data,2)")
#Get the result set as a tuple:
result = cursor.fetchall()

#Iterate through results and print:
for record in result:
    print record
print "Finish."

#Finish dealing with the database and close it
db.commit()
db.close()

但它不起作用,它给了我所有这些

帮助。

4

1 回答 1

1

它应该可以正常工作,您可以使用AND ,因为您只想在右端和左端匹配时才显示它们。但当然US2002014866不匹配JP1014866,因为匹配它应该是US1014866

这是一个SQL Fiddle(我简化了表和列名)

create table table1 (x1 varchar(20), y1 varchar(20)); 
create table table2 (x2 varchar(20), y2 varchar(20)); 
insert into table1 values ('US2002014866','foo1');
insert into table2 values ('US1014866','foo2');
insert into table2 values ('JP1014866','bar');

select *
from   table1
join   table2 on ( left(x1,2)= left(x2,2) and 
                  right(x1,5)=right(x2,5) )

结果只有一行:

X1           Y1   X2        Y2
US2002014866 foo1 US1014866 foo2
于 2013-05-22T18:29:35.133 回答