网络编程
位置:首页>> 网络编程>> 数据库>> Python实现查找数据库最接近的数据

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

作者:青女素娥  发布时间:2024-01-21 08:27:53 

标签:Python,数据库,查找

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()

运行结果为

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

来源:https://www.cnblogs.com/qingnvsue/p/13051202.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com