2022年 11月 9日

python之mysql查询

前置信息:

  1. 数据库地址='localhost'
  2. 数据库用户名='root'
  3. 数据库密码='root'
  4. 数据库名称='test'

数据库中数据信息

  1. id name gongzi
  2. 1 张三 100
  3. 2 李四 200
  4. 3 王二麻子 300
  5. 4 李晓天 400

从user表中获取一条信息

从test数据库user表中获取第一个数据:

  1. import pymysql.cursors
  2. #数据库链接信息
  3. db = pymysql.connect(host='localhost',
  4. user='root',
  5. password='root',
  6. database='test')
  7. # 使用 cursor() 方法创建一个游标对象 cursor
  8. cursor = db.cursor()
  9. # SQL 查询语句,查询user表
  10. sql = 'select * from user '
  11. #执行sql语句查询
  12. cursor.execute(sql)
  13. #这是获取表中第一个数据
  14. rest=cursor.fetchone()
  15. print(rest)
  16. # 关闭数据库连接
  17. db.close()

返回结果:

(1, '张三', '100')

获取表中的全部数据:

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. # SQL 查询语句,查询user表
  9. sql = 'select * from user '
  10. cursor.execute(sql)
  11. #这是查询表中所有的数据
  12. rest=cursor.fetchall()
  13. for i in rest:
  14. print(i)
  15. # 关闭数据库连接
  16. db.close()

执行结果:

  1. (1, '张三', '100')
  2. (2, '李四', '200')
  3. (3, '王二麻子', '300')
  4. (4, '李晓天', '400')

获取某字段全部信息:

例如获取user表中name字段下的所有姓名:

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. # SQL 查询语句,查询user表中name字段中的数据
  9. sql = 'select name from user '
  10. cursor.execute(sql)
  11. #这是查询表中所有的数据
  12. rest=cursor.fetchall()
  13. for i in rest:
  14. print(i)
  15. # 关闭数据库连接
  16. db.close()

执行结果:

  1. ('张三',)
  2. ('李四',)
  3. ('王二麻子',)
  4. ('李晓天',)

条件查询-返回全部信息:

例如获取工资大于100的数据信息:

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. #查询工资大于100的数据
  9. #格式: selet * form 表名字 where 条件
  10. sql = 'select * from user WHERE gongzi>100'
  11. cursor.execute(sql)
  12. #这是查询表中所有的数据
  13. rest=cursor.fetchall()
  14. for i in rest:
  15. print(i)
  16. # 关闭数据库连接
  17. db.close()

执行结果:

  1. (2, '李四', '200')
  2. (3, '王二麻子', '300')
  3. (4, '李晓天', '400')

条件查询B-只返回符合条件的名字

例如,查询test数据库user表中,工资大于100的名字都是有哪些;

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. #查询工资大于100的数据
  9. #格式: selet * form 表名字 where 条件
  10. sql = 'select name from user WHERE gongzi>100'
  11. cursor.execute(sql)
  12. #这是查询表中所有的数据
  13. rest=cursor.fetchall()
  14. for i in rest:
  15. print(i)
  16. # 关闭数据库连接
  17. db.close()

执行结果:

  1. ('李四',)
  2. ('王二麻子',)
  3. ('李晓天',)

数据库条件筛选:

例如,查询test数据库user表中,工资大于等于100,且小于等于400的用户信息,也就是某条件范围筛选;

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. #条件条范围筛选
  9. # sql="select * from user where name = '李四'"
  10. sql="select * from user where gongzi>=200 and gongzi<=400"
  11. cursor.execute(sql)
  12. #这是查询表中所有的数据
  13. rest=cursor.fetchall()
  14. for i in rest:
  15. print(i)
  16. # 关闭数据库连接
  17. db.close()

执行结果:

  1. (2, '李四', '200')
  2. (3, '王二麻子', '300')
  3. (4, '李晓天', '400')

模糊查询

例如查询,name字段下的数据,哪些用户的名字包含[李]姓,并且返回全部信息;

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. #模糊查询,查询名字中包含姓李的用户信息;
  9. sql="select * from user where name like '%李%'"
  10. cursor.execute(sql)
  11. #这是查询表中所有的数据
  12. rest=cursor.fetchall()
  13. for i in rest:
  14. print(i)
  15. # 关闭数据库连接
  16. db.close()

返回信息:

  1. (2, '李四', '200')
  2. (4, '李晓天', '400')

精确查询

查询名称为:李四的用户信息

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. #条件精确查询
  9. # sql="select * from user where name = '李四'"
  10. sql="select * from user where name = '李四'"
  11. cursor.execute(sql)
  12. #这是查询表中所有的数据
  13. rest=cursor.fetchall()
  14. for i in rest:
  15. print(i)
  16. # 关闭数据库连接
  17. db.close()

执行结果:

(2, '李四', '200')

查询多个用户信息

当然也可以同时查询多个用户的信息:

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. #条件精确查询
  9. # sql="select * from user where name = '李四'"
  10. sql="select * from user where name = '李四' or name = '李晓天'"
  11. cursor.execute(sql)
  12. #这是查询表中所有的数据
  13. rest=cursor.fetchall()
  14. for i in rest:
  15. print(i)
  16. # 关闭数据库连接
  17. db.close()

执行结果:

  1. (2, '李四', '200')
  2. (4, '李晓天', '400')

如果想查询李四和李晓天的信息,但是只返回名字和工资信息,并不返回id信息,则可以执行下面的python代码;

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. #条件精确查询
  9. # sql="select * from user where name = '李四'"
  10. sql="select name,gongzi from user where name = '李四' or name = '李晓天'"
  11. cursor.execute(sql)
  12. #这是查询表中所有的数据
  13. rest=cursor.fetchall()
  14. for i in rest:
  15. print(i)
  16. # 关闭数据库连接
  17. db.close()

执行结果:

  1. ('李四', '200')
  2. ('李晓天', '400')

查询没有奖金的用户

此时数据库信息为下方信息;

  1. (1, '张三', '100', )
  2. (2, '李四', '200', '1000')
  3. (3, '王二麻子', '300', '2000')
  4. (4, '李晓天', '400', )

查询没有奖金的用户代码

  1. import pymysql.cursors
  2. db = pymysql.connect(host='localhost',
  3. user='root',
  4. password='root',
  5. database='test')
  6. # 使用 cursor() 方法创建一个游标对象 cursor
  7. cursor = db.cursor()
  8. #条件查询
  9. sql="select * from user where jiangjin<=>NULL"
  10. cursor.execute(sql)
  11. #这是查询表中所有的数据
  12. rest=cursor.fetchall()
  13. for i in rest:
  14. print(i)
  15. # 关闭数据库连接
  16. db.close()

执行结果:

  1. (1, '张三', '100', None)
  2. (4, '李晓天', '400', None)