95. python高级------MySQL数据库 (where查询)

95. python高级------MySQL数据库 (where查询)

python修炼第二十三天

2019年 4月 22日 晴

1. where条件查询的介绍

使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中。

where语句支持的运算符:

  • 比较运算符
  • 逻辑运算符
  • 模糊查询
  • 范围查询
  • 空判断

where条件查询语法格式如下:

select * from 表名 where 条件;
例:
select * from students where id = 1;
2. 比较运算符查询
  • 等于: =
  • 大于: >
  • 大于等于: >=
  • 小于: <
  • 小于等于: <=
  • 不等于: != 或 <>
(1)例1:查询编号大于3的学生:

select * from students where id > 3;

(2)例2:查询编号不大于4的学生:

select * from students where id <= 4;

(3)例3:查询姓名不是“黄蓉”的学生:

select * from students where name != '黄蓉';

(4)例4:查询没被删除的学生:

select * from students where is_delete=0;

3. 逻辑运算符查询
  • and
  • or
  • not
(1)例1:查询编号大于3的女同学:

select * from students where id > 3 and gender=0;

(2)例2:查询编号小于4或没被删除的学生:

select * from students where id < 4 or is_delete=0;

(3)例3:查询年龄不在10岁到15岁之间的学生:

select * from students where not (age >= 10 and age <= 15);

说明:
多个条件判断想要作为一个整体,可以结合‘()’。

4. 模糊查询
  • like是模糊查询关键字
  • %表示任意多个任意字符
  • _表示一个任意字符
(1)例1:查询姓黄的学生:

select * from students where name like '黄%';

(2)例2:查询姓黄并且“名”是一个字的学生:

select * from students where name like '黄_';

(3)例3:查询姓黄或叫靖的学生:

select * from students where name like '黄%' or name like '%靖';

5. 范围查询
  • between … and … 表示在一个连续的范围内查询
  • in 表示在一个非连续的范围内查询
例1:查询编号为3至8的学生:

select * from students where id between 3 and 8;

例2:查询编号不是3至8的男生:

select * from students where (not id between 3 and 8) and gender='男';

6. 空判断查询
  • 判断为空使用: is null
  • 判断非空使用: is not null
例1:查询没有填写身高的学生:

select * from students where height is null;

注意:

  • 不能使用 where height = null 判断为空
  • 不能使用 where height != null 判断非空
  • null 不等于 ‘’ 空字符串
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您讲解如何使用 Python 编写一个通讯录程序,并使用 MySQL 数据库进行数据存储和管理。 首先,您需要安装 MySQL 数据库,并创建一个名为“address_book”的数据库。在该数据库中,创建一个名为“contacts”的表,该表包含以下字段:id、name、phone_number、email。 接下来,您需要安装 Python 的 MySQL 连接器,可以使用以下命令进行安装: ``` pip install mysql-connector-python ``` 然后,您可以使用以下代码连接到 MySQL 数据库: ```python import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="address_book" ) mycursor = mydb.cursor() ``` 接下来,您可以编写代码来添加、编辑、删除和查找联系人。以下是一个简单的添加联系人函数示例: ```python def add_contact(name, phone_number, email): sql = "INSERT INTO contacts (name, phone_number, email) VALUES (%s, %s, %s)" val = (name, phone_number, email) mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record inserted.") ``` 该函数将姓名、电话号码和电子邮件作为参数,并将其插入到“contacts”表中。 您还可以编写其他函数,例如编辑联系人、删除联系人和查找联系人。以下是一个简单的查找联系人函数示例: ```python def find_contact(name): sql = "SELECT * FROM contacts WHERE name = %s" val = (name,) mycursor.execute(sql, val) result = mycursor.fetchall() if len(result) == 0: print("No contacts found.") else: for contact in result: print(contact) ``` 该函数将姓名作为参数,并在“contacts”表中查找该姓名的联系人。如果找不到联系人,则显示“未找到联系人”消息。否则,将显示与该姓名匹配的所有联系人的详细信息。 希望这些信息对您有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值