DML,DQL
上一节 链接: 数据库学习2-操作表、修改列
一、DML:增删改表中的数据
mysql> show tables; -- 查看表
+------------------+
| Tables_in_abc001 |
+------------------+
| abc01 |
| abc02 |
+------------------+
5 rows in set (0.00 sec)
mysql> drop table abc01;-- 删除abc01表;
Query OK, 0 rows affected (0.00 sec)
mysql> create table abc01(id int(99),name varchar(37),password varchar(77)); -- 创建表abc01
Query OK, 0 rows affected (0.00 sec)
mysql> desc abc01; -- 查看表结构
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(99) | YES | | NULL | |
| name | varchar(37) | YES | | NULL | |
| password | varchar(77) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table abc01 add chuan varchar(99); -- 在表中添加一列
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
1.添加数据:
- 语法:
insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);
- 注意:
1.列名和值要一一对应。
2.如果表名后,不定义列名,则默认给所有列添加值。
insert into 表名 values(值1,值2,...值n);
mysql> create table abc003(id int,name varchar(37),age int); -- 创建表
mysql> show tables;
+------------------+
| Tables_in_abc001 |
+------------------+
| ab001 |
| abc003 |
| abc004 |
+------------------+
mysql> desc abc003; -- 查询表结构
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(37) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
mysql> insert into abc003 values(1,'cai',18),(2,'chuan',20),(3,'qi',30); -- 增加列
mysql> select * from abc003; -- 查询列
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | l | 18 |
| 2 | c | 20 |
| 3 | y | 30 |
+------+-------+------+
mysql> insert into abc01 (id,name,password) values(2,"abc","12345");-- insert into 表名(列名1,列名2) values (值1,值2) 这里的列名可以不加,如果不加的话,后面的值就需要匹配所有的列,如果加列名,后面的值需要一一匹配;
Query OK, 1 row affected (0.00 sec)
3.除了数字类型,其他类型需要使用引号(单双都可以)引起来
2.删除数据:
- 语法:
delete from 表名 [where 条件]
mysql> show tables;
+------------------+
| Tables_in_abc001 |
+------------------+
| abc003 |
| abc004 |
+------------------+
2 rows in set (0.00 sec)
mysql> desc abc003;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(37) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from abc003;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | l | 18 |
| 2 | c | 20 |
| 3 | y | 30 |
+------+-------+------+
3 rows in set (0.00 sec)
mysql> delete from abc003 where id=1; -- 删除列中的值,使用的是delete from 表名 where 判断(对列的判断选择)
Query OK, 1 row affected (0.45 sec)
mysql> delete from abc01 where name='name1';-- 如果是字符串类型的,需要在后加上引号;不然无法识别;
Query OK, 4 rows affected (0.00 sec)
mysql> select * from abc003; --查询列
+------+-------+------+
| id | name | age |
+------+-------+------+
| 2 | c | 20 |
| 3 | y | 30 |
+------+-------+------+
- 注意:
如果不加条件,则删除表中所有记录。
如果要删除所有记录
- delete from 表名;-- 不推荐使用。有多少条记录就会执行多少次删除操作。
- TRUNCATE TABLE 表名; – 推荐使用,效率更高 先删除表,然后再创建一张一样的表。
3.修改数据
- *语法:
update 表名 set 列名1 = 值1,列名2 = 值2,...[where 条件];
mysql> update abc003 set age=99 where id=3; -- 修改列中的数据,使用update 表明 set 列名1=值1(这里的列和值是被修改后的数据) where id=1(这里是一个条件语句,id>3也是可以的)
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from abc003;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 2 | c | 20 |
| 3 | y | 99 |
+------+-------+------+
2 rows in set (0.00 sec)
- 注意:
如果不加任何条件,则会将表中所有记录全部修改。
二、DQL:查询表中的字段
create table Student(id varchar(10),name varchar(10),age datetime,sex varchar(10),primary key(id))
insert into Student values('01' , '赵雷' , '1990-01-01' , '男')
insert into Student values('02' , '钱电' , '1990-12-21' , '男')
insert into Student values('03' , '孙风' , '1990-05-20' , '男')
insert into Student values('04' , '李云' , '1990-08-06' , '男')
insert into Student values('05' , '周梅' , '1991-12-01' , '女')
insert into Student values('06' , '吴兰' , '1992-03-01' , '女')
insert into Student values('07' , '郑竹' , '1989-07-01' , '女')
insert into Student values('08' , '王菊' , '1990-01-20' , '女')
mysql> show tables; -- 查看表
+------------------+
| Tables_in_abc001 |
+------------------+
| abc01 |
| abc02 |
| abc03 |
+------------------+
5 rows in set (0.01 sec)
mysql> desc abc03;-- 查看列
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> select * from users;
+----+----------+----------------------------------+-------+------+
| id | username | password | level | age |
+----+----------+----------------------------------+-------+------+
| 1 | admin | 202cb962ac59075b964b07152d234b70 | 1 | NULL |
mysql> alter table abc03 add name varchar(4); -- 修改列
Query OK, 0 rows affected (0.01 sec)
mysql> desc abc03;-- 查看列
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into abc03 values (1,'na1'); -- 添加数据
Query OK, 1 row affected (0.00 sec)
mysql> select * from abc03; -- 查看数据
+------+------+
| id | name |
+------+------+
| 1 | na1 |
| 2 | na2 |
| 2 | na3 |
| 3 | na3 |
| 4 | na3 |
+------+------+
5 rows in set (0.00 sec)
mysql> update abc03 set name="nam7" where id>=2; -- 修改数据
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> delete from abc03 where id=2; -- 删除数据
Query OK, 2 rows affected (0.00 sec)
select * from 表名;
1.语法
select
字段列表
from
表名列表
where
条件列表
group by
分组字段
having
分组之后的条件
order by
排序
limit
分页限定
group_concat()
分组查询
2.基础查询
1.多个字段的查询
select 字段名1,字段名2... from 表名;
mysql> insert into abc003 values(1,'l',18),(4,'c',30),(5,"ddd",66);
Query OK, 3 rows affected (0.13 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from abc003;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | l | 18 |
| 2 | c | 20 |
| 3 | y | 99 |
| 4 | test | 30 |
| 5 | ddd | 66 |
+------+-------+------+
5 rows in set (0.00 sec)
mysql> select id,name from abc003;
+------+-------+
| id | name |
+------+-------+
| 1 | l |
| 2 | c |
| 3 | y |
| 4 | test |
| 5 | ddd |
+------+-------+
5 rows in set (0.10 sec)
- 注意:
如果查询所有字段,则可以使用*来替代字段列表。
2.去除重复
distinct
SELECT DISTINCT country FROM Websites;
SELECT * FROM Websites WHERE country='CN'AND alexa > 50;
SELECT * FROM Websites WHERE alexa > 15 AND (country='CN' OR country='USA');
3.计算列
一般可以使用四则运算计算一些列的值。(一般只会进行数值型的计算)
ifnull(表达式1,表达式2);
- 表达式1:那个字段需要判断是否为null
- 如果改字段为null后的替换值。
4.起别名
as :as也可以省略
3.条件查询where
1.where子句后跟条件
2.运算符
>,<,>=,<=,=,<>
BETWEEN...AND
IN(集合)
LIKE
IS NULL
and or &&
or or ||
not or !
mysql> select * from abc003 where id=2;+------+-------+------+
| id | name | age |
+------+-------+------+
| 2 | c | 20 |
+------+-------+------+
1 row in set (0.12 sec)
mysql> select * from abc003 where id>1 and id<4;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 2 | c | 20 |
| 3 | y | 99 |
+------+-------+------+
2 rows in set (0.10 sec)
4.其他
1.排序查询
语法:order by 子句
order by 排序字段1 排序方式1,排序字段2 排序方式2…
mysql> select * from abc003 order by name;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | l | 18 |
| 2 | c | 20 |
| 5 | ddd | 66 |
| 3 | y | 99 |
| 4 | test | 30 |
+------+-------+------+
5 rows in set (0.10 sec)
mysql> select * from abc003 order by id desc; -- 默认是正序排序,如果加上desc 就可以进行反序的排序;
+------+-------+------+
| id | name | age |
+------+-------+------+
| 5 | ddd | 66 |
| 4 | test | 30 |
| 3 | y | 99 |
| 2 | c | 20 |
| 1 | l | 18 |
+------+-------+------+
5 rows in set (0.00 sec)
mysql> select * from abc01 where id>5 and id<9 order by id;
+------+-------+-----------+------+-------+
| id | name | password | cai | chuan |
+------+-------+-----------+------+-------+
| 6 | name6 | password6 | NULL | NULL |
| 7 | name7 | password7 | NULL | NULL |
| 8 | name8 | password8 | NULL | NULL |
+------+-------+-----------+------+-------+
3 rows in set (0.00 sec)
- 排序方式:
ASC:升序,默认的
DESC:降序
- 注意:
如果有多个排序条件,则当前的条件值一样时,才会判断第二条件。
2.聚合函数:将一列数据作为一个整体,进行纵向的计算。
- 1.count:计算个数
一般选择非空的列:主键 count(*)
mysql> select count(*) as age from abc003; -- 计算表abc003中的age的个数是多少个,默认去掉NULL值;
+-----+
| age |
+-----+
| 5 |
+-----+
1 row in set (0.00 sec)
mysql> select count(l) as l from abc01;
+-----+
| l |
+-----+
| 1 |
+-----+
1 row in set (0.00 sec)
- 2.max:计算最大值
- 3.min:计算最小值
- 4.sum:计算和
- 5.avg:计算平均值
mysql> select max(id) from abc01;
+---------+
| max(id) |
+---------+
| 9 |
+---------+
1 row in set (0.00 sec)
mysql> select min(id) from abc01;
+---------+
| min(id) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
mysql> select sum(id) from abc01;
+---------+
| sum(id) |
+---------+
| 50 |
+---------+
1 row in set (0.00 sec)
mysql> select avg(id) from abc01;
+---------+
| avg(id) |
+---------+
| 5.0000 |
+---------+
1 row in set (0.00 sec)
- 注意:聚合函数的计算,排除null值。
解决方案:
选择不包含非空的列进行计算
IFNULL函数
3.分组查询
- 语法:group by 分组字段
mysql> select name,sum(age) from abc003 group by name;
+-------+----------+
| name | sum(age) |
+-------+----------+
| l | 18 |
| c | 20 |
| ddd | 66 |
| y | 99 |
| test | 30 |
+-------+----------+
5 rows in set (0.00 sec)
- 注意:
- 1.分组之后查询的字段:分组字段、聚合函数
- 2.where 和 having的区别?
1.where 在分组之前进行限定,如果不满足条件,则不参与分组。having在分组之后进行限定,如果不满足结果,则不会被查询出来
2.where后不可以跟聚合函数,having可以进行聚合函数的判断。
4.分页查询
- 语法:limit开始的索引,每页查询的条数;
公式:开始的索引 = (当前的页码 - 1) * 每页显示的条数
--每页显示3条记录
SELECT * FROM student LIMIT 0.3; --第1页
SELECT * FROM student LIMIT 3.3; --第2页
SELECT * FROM student LIMIT 6.3; --第3页
limit 是一个MySQL"方言"
mysql> select * from abc003 limit 1;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | l | 18 |
+------+------+------+
1 row in set (0.04 sec)
mysql> select * from abc003 limit 3;-- 显示前3 列数据
+------+-------+------+
| id | name | age |
+------+-------+------+
| 1 | l | 18 |
| 2 | c | 20 |
| 3 | y | 99 |
+------+-------+------+
3 rows in set (0.00 sec)
链接: 参考学习–菜鸟教程runoob
标签:set,name,改表中,--,mysql,sec,表中,增删,id From: https://blog.csdn.net/qq_72421395/article/details/140886983