当前位置:主页 > 软件编程 > Python代码 >

Python实现查找数据库最接近的数据

时间:2021-09-07 09:11:11 | 栏目:Python代码 | 点击:

这是我的数据库student,好比输入一个值为32,查询id最接近32的整行数据,可以用以下代码

import pymysql
value=32

conn = pymysql.connect(host='39.106.168.84', user='xxxxxxx', password='xxxxxxx', port=3306,
            db='flask_topvj_net')
cur = conn.cursor()
sql="select * from `student` order by abs(`id` - '%s') limit 1" %(value)
cur.execute(sql)
u = cur.fetchall()
print(u)
conn.close()

运行结果为

您可能感兴趣的文章:

相关文章