千家信息网

3Python全栈之路系列之MySQL表内操作

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,Python全栈之路系列之MySQL表内操作先创创建一个表用于测试-- 创建数据库CREATE DATABASE dbname DEFAULT CHARSET utf8 COLLATE utf8_ge
千家信息网最后更新 2026年11月01日3Python全栈之路系列之MySQL表内操作

Python全栈之路系列之My


SQL表内操作


先创创建一个表用于测试

-- 创建数据库CREATE DATABASE dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;-- 创建表CREATE TABLE `tb` (  `id` int(5) NOT NULL AUTO_INCREMENT,  `name` char(15) NOT NULL,  `alias` varchar(10) DEFAULT NULL,  `email` varchar(30) DEFAULT NULL,  `password` varchar(20) NOT NULL,  `phone` char(11) DEFAULT '13800138000',  PRIMARY KEY (`id`,`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;

增加表内数据

 进入dbname数据库mysql> use dbnameDatabase changed# 查看当前库所有的表mysql> show tables;+------------------+| Tables_in_dbname |+------------------+| tb               |+------------------+1 row in set (0.00 sec)# 查看tb表内的内容mysql> select * from tb;Empty set (0.00 sec)
-- 插入单条数据insert into tb(name,email,password) values("ansheng","anshengme.com@gmail.com","as");-- 同时插入多条数据insert into tb(name,email,password) values("as","i@anshengme.com","pwd"),("info","info@anshengme.com","i");

查看插入的数据

mysql> select * from tb;+----+---------+-------+-------------------------+----------+-------------+| id | name    | alias | email                   | password | phone       |+----+---------+-------+-------------------------+----------+-------------+|  2 | ansheng | NULL  | anshengme.com@gmail.com | as       | 13800138000 ||  3 | as      | NULL  | i@anshengme.com         | pwd      | 13800138000 ||  4 | info    | NULL  | info@anshengme.com      | i        | 13800138000 |+----+---------+-------+-------------------------+----------+-------------+3 rows in set (0.00 sec)

把别的表的数据插入当前表

查看tb_copy表内的内容

mysql> select * from tb_copy;+----+--------+-------+-------+----------+-------------+| id | name   | alias | email | password | phone       |+----+--------+-------+-------+----------+-------------+|  5 | hello  | NULL  | NULL  | 1        | 13800138000 ||  6 | word   | NULL  | NULL  | 2        | 13800138000 ||  7 | python | NULL  | NULL  | 3        | 13800138000 |+----+--------+-------+-------+----------+-------------+3 rows in set (0.00 sec)

把tb_copy表内的name,email,password列插入到tb表中

insert into tb (name, email, password) select name,email,password from tb_copy;

查询tb内的内容

mysql> select * from tb;+----+---------+-------+-------------------------+----------+-------------+| id | name    | alias | email                   | password | phone       |+----+---------+-------+-------------------------+----------+-------------+|  2 | ansheng | NULL  | anshengme.com@gmail.com | as       | 13800138000 ||  3 | as      | NULL  | i@anshengme.com         | pwd      | 13800138000 ||  4 | info    | NULL  | info@anshengme.com      | i        | 13800138000 ||  5 | hello   | NULL  | NULL                    | 1        | 13800138000 ||  6 | word    | NULL  | NULL                    | 2        | 13800138000 ||  7 | python  | NULL  | NULL                    | 3        | 13800138000 |+----+---------+-------+-------------------------+----------+-------------+6 rows in set (0.00 sec)

删除表内数据

-- 删除表内的所有内容delete from tb_copy;
-- 删除表内某一条数据delete from tb where id=2 and name="ansheng";

更改表内数据

update tb set name="as" where id="3";

查

-- 查询表内所有内容select * from tb;-- 带条件的查询表内的内容select * from tb where id > 4;

查询的时候指定最后一列的名称

mysql> select id,name as username from tb where id > 4;+----+----------+| id | username |+----+----------+|  5 | hello    ||  6 | word     ||  7 | python   |+----+----------+3 rows in set (0.00 sec)

其他操作

条件

-- 多条件查询select * from tb where id>3 and name="hello" and password="1";-- 查询指定范围select * from tb where id between 4 and 6;-- 查询括号内存在的数据select * from tb where id in (4,6);-- 查询括号内不存在的数据select * from tb where id not in (4,6);-- 以别的表的内容为查询条件select * from tb where id in (select id from tb_copy);

通配符

-- 以p开头的所有(多个字符串)select * from tb where name like "p%";-- 以p开头的所有(一个字符)select * from tb where name like "p%";

限制

-- 前三行数据select * from tb limit 3;-- 从第2行开始的3行select * from tb limit 2,3;-- 从第4行开始的5行select * from tb limit 5 offset 4;

排序

-- 根据"name"列从小到大排列select * from tb order by name asc;-- 根据"name"列从大到小排列select * from tb order by name desc;-- 根据 "列1" 从大到小排列,如果相同则按列2从小到大排序select * from 表 order by 列1 desc,列2 asc;

分组

select id from tb group by id;select id,name from tb group by id,name;select num,nid from 表  where nid > 10 group by num,nid order nid desc;select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid;select num from 表 group by num having max(id) > 10;

特别的:group by 必须在where之后,order by之前

连表

无对应关系则不显示

select A.num, A.name, B.name from A,B where A.nid = B.nid;

无对应关系则不显示

select A.num, A.name, B.name from A inner join B on A.nid = B.nid;

A表所有显示,如果B中无对应关系,则值为null

select A.num, A.name, B.name from A left join B on A.nid = B.nid;

B表所有显示,如果B中无对应关系,则值为null

select A.num, A.name, B.name from A right join B on A.nid = B.nid;

组合

组合,自动处理重合

select nickname from A union select name from B;

组合,不处理重合

select nickname from A union all select name from B;

#Python全栈之路


数据 查询 内容 条件 组合 之路 从小到大 从小 字符 开头 括号 数据库 查询表 处理 排序 相同 内存 同时 名称 多个 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网新科技 互联网巨头科技公司 北京互联网科技 贷财互联网科技 互联网女皇 腾讯科技 赫美联合互联网科技 上游互联网科技 互联网 新科技 博兴的互联网电子科技公司 阿里巴巴算科技企业还是互联网 互联网系的金融科技公司 恒生互联网科技指数跳水 河南住房之家互联网科技 佛山市翊博互联网络科技有限公司 长沙茂晟互联网科技有限公司 吉林省码上互联网科技电话 广西科技产业互联网是什么 银川新华互联网科技学校原画 科技互联网公司两年变化 保山浩联互联网科技有限公司 与互联网科技公司 华脉科技工业互联网产品 中国最先进互联网科技公司 湖北码尼互联网科技有限公司电话 未来五年的互联网科技 用友工业互联网科技发展 上海创道互联网络科技有限公司 互联网科技年终奖 吉林省码上互联网科技电话 张家口科技产业互联网平台
0