MySQL数据表的增、删、改、查
SQL语句对大小写不敏感
1. 插入
使用 insert into 关键字
insert into (属性名,属性名…) values(值,值…);
insert into (ct_id,ct_name) values (1,'玩具');
如果想要插入多行
insert into (ct_id,ct_name) values (2,'文具') ,(3,'书籍');
如果插入成功,MySQL会提醒:
Records: 3 Duplicates: 0 Warnings: 0
重复插入会报的错误:
mysql> insert into commoditytype (ct_id,ct_name) values (1,‘服装’);
ERROR 1062 (23000): Duplicate entry ‘1’ for key ‘PRIMARY’
2. 删除
使用 delete 关键字
delete from 表名 where 删除条件;
delete from commoditytype where ct_id=4;
如果不写删除条件,则会将表中记录全部删除。
3. 修改
使用 update 关键字
update 表名 set 修改的值 where 修改条件;
update commoditytype set ct_name='书籍' where ct_id=3;
Rows matched: 1 Changed: 1 Warnings: 0
如果不写修改条件,则会将表中数据修改成一样的值。
delete from commoditytype ;
4. 查
单表查询
使用select关键字。
mysql> #mysql中 使用’'来标识值 使用``来标识字段或者表名称
select ‘helloworld’ ;
±-----------+
| helloworld |
±-----------+
| helloworld |
±-----------+
1 row in set (0.00 sec)在这里select相当于输出
查询全表:
select * from 表名;
select * from commoditytype;
其中,(*)是选取所有列
查询想要的字段:
select 字段名 from 表名 where 查询条件;在查询多个字段时,字段名用逗号(,)隔开。
select c_name,c_madein,c_outprice-c_inprice
from commodity
where c_outprice-c_inprice>100;
MySQL中可以进行简单的四则运算。
当null参与运算的时候,结果也为null
在查询中,DISTINCT关键字可以去掉重复数据
select distinct 字段名,字段名… from 表名;
select distinct c_type from commodity;
1.带关系运算符和逻辑运算符的条件数据查询
算数运算符 | 描述 | 逻辑运算符 | 描述 |
---|---|---|---|
> | 大于 | AND(&&) | 逻辑与 |
< | 小于 | OR( | |
= | 等于 | XOR | 逻辑异或 |
!=(<>) | 不等于 | NOT(!) | 逻辑非 |
<= | 小于等于 | ||
>= | 大于等于 |
select c_name,c_inprice
from commodity
where (c_inprice>=10 and c_inprice<=100) and c_type=3;
2.带BETWEEN AND关键字的条件查询语句
between and 是包括两个数字的
不是10-100之间 c_inprice<=10 or c_inprice>=100
between 10 and 100 是包括10和100的
select c_name,c_inprice
from commodity
where (c_inprice between 10 and 100) and c_type=3;
not between and 是这个区间范围外的查询
not between 10 and 100是不包括10和100的
select c_name,c_inprice
from commodity
where c_inprice not between 10 and 100;
3.带IS NULL关键字的条件查询语句
is null 为空判断
is not null 非空判断
select c_name,c_type
from commodity
where c_outprice is null;
4.带IN关键字的条件查询语句
in 关键字 用来检索特点的某些值
select c_name,c_inprice
from commodity
where c_inprice in (10,20,30,40,50);
不在以上这些数字里面用 not in
select c_name,c_inprice from commodity
where c_inprice not in (10,20,30,40,50);
c_inprice in (10,20,30,40,50)相当于c_inprice=10 or c_inprice=20…
而c_inprice not in (10,20,30,40,50)相当于c_inprice<>10 and c_inprice<>20…
5.带LIKE关键字的条件查询语句;
使用like关键字,进行模糊匹配
LIKE要和通配符一起用,不然没有用通配符的LIKE效果等同于“=”,常用的通配符有“_”和“%”;
_:表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:(可以代表一个中文字符)
%:表示任意个或多个字符。可匹配任意类型和长度的字符。
比如"_a%"表示第一个字母任意,第二个字母必须是a,后面字母任意,复合的结果有ward,martin,james等
select c_name from commodity
where c_name like '%玩具%';
like ‘孙’ 效果等同于 =‘孙’
like ‘%’ 效果等同于 查询所有
如果要查"%"或者 "",使用"escape",转义字符后面的"%“或”“就不作为通配符了,注意:前面没有转义字符的”%“和”_"仍然起通配符作用
6. 排序
排序的关键字是ORDER BY,后面跟ASC(升序,可以不写默认)
或者DESC逆序;
select c_name,c_inprice from commodity
order by c_inprice ;
在排序中,如果有空值,ASC(升序)中空值排在最前,DESC(降序)中空值排在最后。
ORDER BY 可与LIMIT关键字一直使用。LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。
举个例子吧:
select * from table limit 2; 只输出前2行;
select * from table limit 0,5;从第0行开始,输出5行;
select * from table order by sal limit 1;按照sal列升序排序
select c_name,c_inprice
from commodity
order by c_inprice desc
limit 5;
可以使用它来做排行榜,来进行分页。
如果使用它来分页,随着数据量的增加,页数会越来越多,就是越往后分页,LIMIT语句的偏移量就会越大,速度也会明显变慢。这时要
数据库优化
所以在分页的时候,最好不要直接使用limit,而是先获取到索引,使用limit来获取数据。效果会好很多。
其中尽量避免使用LIKE关键字和通配符,在建有索引的字段上尽量不要使用函数进行操作,是数据库优化的一种方法。
7. 统计函数
COUNT()函数:统计记录数;
select count(*) from commodity where c_type=1;
count(*)使用方式,实现对表中记录进行统计,不管是否包含NULL还是NOT NULL
count(field)使用方法对指定字段进行统计,将忽略NULL值!
AVG()函数:求平均值;
SUM()函数:求和;
MAX()函数:求最大数;
MIN()函数:求最小数;
select avg(c_inprice) from commodity where c_type=4;
select avg(c_outprice),sum(c_outprice) from commodity;
其中,统计函数会自动去除条件中的null值。
count和其他聚合函数的区别:
当没有结果的时候 其他函数返回null count返回0
8. 分组
group by 进行分组查询
group by 一般和 聚合函数一起出现
select * from commodity group by c_type;
select c_type, sum(c_inprice),avg(c_inprice),max(c_outprice)
from commodity
group by c_type;
在mysql中,要对分组查询结果进行条件限制查询,不能使用WHERE关键字,需要使用HAVING关键字。
select c_type , avg(c_inprice)
from commodity
group by c_type
having avg(c_inprice)>100;
注意!having中的条件,即使查询条件没有出现在结果表中,依然可以查询出结果。
select c_type
from commodity
group by c_type
having avg(c_inprice)>100;
这个having中不需要出现在结果表中 是从MySQL5.7版本才开始支持的。