SQL获取最后5个整数

2024-10-01 07:10:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在两个不同的表中有两列。
PCTNo具有以下值:US2002014866
pct_publishing_data具有以下值:JP1014866
我想得到PCTNo的最后5个值和pct_publishing_data的最后5个值,如果pct_publishing_data的5个数字在PCTNo中,前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()

但它不起作用,它给了我全部

救命啊。你知道吗


Tags: tosampledbdataresultcursorusprint
1条回答
网友
1楼 · 发布于 2024-10-01 07:10:39

它应该可以正常工作,您可以使用AND,因为您只想在右端和左端匹配时显示它们。但当然US2002014866JP1014866不匹配,因为匹配它应该是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

相关问题 更多 >