MySQL数据库 数据的更新

有表A,B,有name,sal,deptNo
1,数据更新
update
update A set sal=500 where name='zhangsan';(一次可更改多个值,用逗号隔开)
2,数据的删除
delete
delete from A where name='zhangsan';(删除一行数据)
delete from A where A_age between c and d;(删除c到d之间的所有数据)
3,增加

insert

insert into A values(id int ,name varchar(20),sal deciml,deptNo int);

insert into B(name,sal) values(name varchar(20),sal deciml);

4,查询(DQL)
a,select {*|列名,...} from A; (*号查询A表里所有数据,列名查询A表里的某一列)
b,设置列名别名:select a.name as 姓名,a.sal as 工资 from A as a;(as可不加)
c,做运算:select a.sal+5000 as 工资 from A a;
d,拼接:select concat(a.name,'&',a.sal) as 工资表 from A a;
e,限制行数显示:select * from A limit 0,3;(limit后跟的两个数字,前者表示从哪行开始,后者表示返回多少行)
f,去重:select distinct sal from A;(排除重复数据)
g,where子句在查询中的用法:
select * from A where sal in(5100,3000);(查询sal为5100和3000两个人的信息)
select * from A where sal not in(4000);(查询除开sal为4000以外的所有人信息)
SELECT * FROM A WHERE (deptNo,sal) in (SELECT deptNo,MAX(sal) FROM A GROUP BY deptNo);(查询每个部门工资最高的人)
h,模糊查询:select * from A where name like 'ma%';
select * from A where name like 'ma%';(查询名字为两个字的人的信息)
通配符:'%'代表0个或多个任意字符
'_'代表一个字符
i,排序查询:select * from A order by name;(升序)
select * from A order by name desc;(降序)
5,聚合函数
a,count函数 select count(*) from A;(A表中全部列的数量)
select count(列名) from A;(此列的行数)
select count(distinct 列名) from A;(去重之后的列的数量)
b,sum函数 select sum(<计数规范>) from A;
max,avg,min与sum用法相同
6,数据分组
select 列a,聚合函数 from 表名 where 条件 group by 列a having 过滤条件;
7,子查询---连表查询
分类 相关子查询:执行依赖于外部查询的数据;非相关子查询
select中嵌套 select a.name,a.sal,(select b.name from B b where a.deptno=b.deptno) from A a;
where中嵌套 select * from A where sal>(select sal from A where name='zhangsan');
8,组合查询--UNION 是一种联合两条或两条以上的查询语句,类似多条查询结果相结合的效果
结构:select 列名1 from A
UNION
select 列名2 from B
表A,表B列数需相同,且查询完毕后,表B列名2被忽略
9,联接
a,表联接 内联接 (inner join) inner可以省略,效率很低
外联接 (left out join,right out join) 其中out可以省略
select a.name,b.name from A a left join B b on 联接条件(外键条件);
b,自联接 所联接的表来自同一个表
select a.name,i.name from A a left join A i on 条件;
10,SQL语句的执行顺序
SELECT 子句
FROM 子句
WHERE 子句
GROUP BY 子句
ORDER BY 子句
HAVING 子句
11,一个数值与null相加得到的数值也为null

原文地址:https://www.cnblogs.com/tushengadbm/p/5493000.html