mysql知识点回顾(二)

索引
唯一索引
 
    唯一:
        约束不能重复(可以为空)
        ps:主键:唯一且不能为空
    加速查找
 
上图为多对多
 
 
SQL语句补充:
create table tb11(
    id int auto_increment primary key,
    name varchar(32),
    age int
)engine=innodb default charset=utf8;
 
insert into tb12(name,age) values('alex',12);
 
insert into tb12(name,age) values('alex',12),('root',18);
 
insert into tb12(name,age) select name,age from tb11;    #将tb11表中的name和age插入到tb表里
delete from tb12;
delete from tb12 where id =2;
delete from tb12 where id !=2;
delete from tb12 where id !=2 and name='alex';
update tb12 set name='alex',where id>12 and name='xx';
update tb12 set name='alex',age =19 where id>12 and name='xx';
 
select * from tb12;
select id,name from tb12;
select id,name from tb12 where id >10 or name = 'xxxx';
select id,name as cname  from tb12 where id >10 or name = 'xxxx'; #给输出的结果,列名设置别名。
select id,name,123 from tb12 where id >10 or name = 'xxxx';  结果后面加了一列123
 
mysql中两种不等于的写法:
 
 
select id,name from tb12 where id in (1,5,12);
select id,name from tb12 where id not in (1,5,12);
select id,name from tb12 where id between 5 and 12;   查询id在5-12之间内容,包含5和12
 
 
mysql通配符
以a开头:
a%   %代表无数个内容
a_     _代表一个内容
 
select * from tb12 where name like "a%"
select * from tb12 limit 0,10;   0是起始位置,10是起始位置开始显示几条。
 
排序:
order by 默认是升序排序
 
select * from tb12 order by id desc;  从大到小
select * from tb12 order by id asc;   从小到大
 
select PROD_DESC,PROD_ID,COST from PRODUCTS_TBL where COST<20 order by 1;
1就是PROD_DESC
2是PROD_ID
3是COST
可以用数字来代替。
 
select * from tb12 order by age desc,id desc;  按age从大到小排,如果有重复让重复的再按id从大到小排。
 
 
分组:
group by
group by 在where之后 order by 之前
select max(id),part_id from userinfo5 group by part_id; 取max(id)和part_id两列,按part_id分组,max取最大,min取最小
 
COUNT函数
select count(id),part_id from userinfo5 group by part_id;  count取个数。
count更有可读性的写法:
select count(part_id ) from userinfo5 group by part_id;
注意:count(*)返回的结果包括重复项和NULL。count统计的是行数,不涉及数据类型。
 
SUM函数
SUM函数只能处理数值字段。
select SUM(COST) from PRODUCTS_TBL;
 
AVG函数
计算平均值,只能处理数字字段
select AVG(SALARY) from EMPLOYEE_PAY_TBL;
 
MAX函数
返回记录中某个最大的值,NULL不在计算范围内
 
MIN函数
最小值
 
汇总函数和DISTINCT命令通常不一起使用。
 
 
 
 
 
 
sql语句,如果group by分组之后后面需要聚合函数则必须使用having,如:
select count(id),max(id),part_id from userinfo5 goup by part_id having count(id)>1;
 
sql语句where后面不能加聚合函数的结果。
 
去重:
select distinct PROD_DESC from CANDY_TBL;    结果去重
 
连表操作:
select * from userinfo5,department5
select * from userinfo5,department5 where userinfo5.part_id = department5.id;
将两张表内容再一张表中显示
 
select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
和上面效果一样,两张表在一张表显示,第二张表显示的顺序条件是按照userinfo5.part_id = department5.id
left join  左边的表会全部显示,右边不会全部显示。
right join 右边的表会全部显示,左边不会全部显示。不显示的为NULL
 
right可以不用,可以用left,但是把两张表位置调换一下,效果可right是一样的。
 
表和表之间必须有关系,有关系才能连表查询,外键等等。
 
 
 
如果通过连表连查多张表,注意列名重复的问题,需要带上表名.列名 这样。如下图:
 
inner join 和left right的区别就是,会将出现null时的一行隐藏。
 
 
 
select count(id) from userinfo5; 数行数
 
 
 
Mysql逻辑操作符
IS NULL     与NULL比较
BETWEEN   包括边界值,between 2 and 3;包括2和3
IN   如where PROD_ID in ("13","9","5");
LIKE  用通配符匹配,%匹配0个一个或多个数字或字符 _下划线代表一个数字或字符
EXISTS 用于搜索指定表里是否存在满足特定条件的记录
如:
select COST
from PRODUCTS_TBL
where exists(
select COST from PRODUCTS_TBL where COST > 100);
 
UNIQUE
ALL  让一个值和一个集合全部比较 
ANY 让一个值和一个集合任意元素比较,如:
select *
from PRODUCTS_TBL
where COST > any(
select COST from PRODUCTS_TBL where COST < 100);
 
 
算数操作符
 
+加法
-减法
*乘法
/ 除法
例如:
select SALARY + BONUS from EMPLOYEE_PAY_TBL;
select SALARY - BONUS from EMPLOYEE_PAY_TBL;
select SALARY * 10 from EMPLOYEE_PAY_TBL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/ArmoredTitan/p/8492536.html