2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
create table students(
id int unsigned primary key auto_increment,
sn int unsigned unique key,
name varchar(20) not null,
qq varchar(32) unique key
);
insert into students values(1, 123, '张飞', '45678');
insert into students (sn, name, qq) values(234, '关羽', '23456');
insert into students values (12, 126, '曹操', '12345'), (13, 127, '许攸', '34567');
insert into students (sn, name, qq) values (128, '孙权', '66666'), (129, '许攸', '88888');
insert into students values (13, 130, '曹操', '111111') on duplicate key update sn=130, name='曹操', qq='111111'
- - 0 ordo affectus est: Data in mensa discrepant, sed valor notitiarum diversarum aequalis est quantitati renovationis.
- - I ordo affectus est: Nulla in mensa diversa notitia, notitia inseritur
- - 2 ordo affectus est: Est in mensa diversa notitia et notitia renovata est
//主键或者唯一键没有冲突,则直接插入;
//主键或者唯一键如果冲突,则删除后再插入
replace into students (sn, name, qq) vlaues (20001, '曹阿瞒', '111222');
- I versu adfectus est: Nulla in mensa diversa notitia, notitia inseritur
- 2 ordo affectus est: Data in mensa discrepat, eam delere et eam agendi rationem reddere
create table exam_result(
id int unsigned primary key auto_increment,
name varchar(20) not null,
chinese float default 0.0,
math float default 0.0,
english float default 0.0
);
insert into exam_result (name, chinese, math, english) values
('唐三藏', 67, 98, 56),
('孙悟空', 87, 78, 77),
('猪悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55, 85, 45),
('孙权', 70, 73, 78),
('宋公明', 75, 65, 30);
//全列查询
select * from exam_result;
//指定列查询
select id from exam_result;
select id, math, name from exam_result;
//查询字段为表达式
select id, name, math+chinese+english as total;
+-----------+-------+
| name | total |
+-----------+-------+
| 唐三藏 | 221 |
| 孙悟空 | 242 |
| 猪悟能 | 276 |
| 曹孟德 | 233 |
| 刘玄德 | 185 |
| 孙权 | 221 |
| 宋公明 | 170 |
+-----------+-------+
7 rows in set (0.00 sec)
//结果去重
select distinct math from exam_result;
comparationis operator | illustrare |
---|---|
>, >=, <, <= | Maior, maior vel aequalis, minor, minor vel aequalis |
= | Aequalis, NULL non est tutum. |
<=> | Aequale, NULL tutum est, verbi causa, effectus NULL <=> NULLUS est VERA(1). |
!=, <> | NON = NULL, NULL non tutum. |
inter A0 et a1 | Range par, [a0, a1], si a0 <= valorem <= a1, ad VERA(1) |
in (optionem, …) | Si quis est ex optionibus, vera redde (1) |
nulla est | nulla est |
non est NULL | Non NULL |
non est NULL | Visne adaptare. % aliquem numerum repraesentat; |
Operator Logicus | illustrare |
---|---|
et | Multiplices condiciones omnes verae esse debent (1) propter eventum ut verum sit. |
or * | Si aliqua conditio VERA est (1), effectus est VERA (I) |
not | Conditionalis negatio vera vel falsa |
//1.查找英语成绩小于60的同学名字和英语成绩
select name, english from exam_result where english<60;
//2.查找语文成绩在[80,90]的同学名字和语文成绩
select name, chinese from exam_result where chinese>=80 and chinese<=90;
//3.查找数学成绩是58或者59或者98或者99的同学名字和数学成绩
select name, math from exam_result where math=58 or math=59 or math=98 or math=99;
//更优雅的写法
select name, math from exam_result where math in (58, 59, 98, 99);
//4.查找姓孙的同学的名字
select name from exam_result where where name like '孙%';
+-----------+
| name |
+-----------+
| 孙悟空 |
| 孙权 |
+-----------+
//5.查找孙某同学的名字
select name from exam_result where name like '孙_';
+--------+
| name |
+--------+
| 孙权 |
+--------+
//6.查找总分在200以下的同学的名字,总分
select name, chinese+math+english as total from exam_result where chinese+math+english<200;
//这样写可以吗?不可以!!!因为先执行where子句,然后才执行select,对总分重命名
//select name, chinese+math+english as total from exam_result where total<200;
//7.查找语文成绩大于80并且不姓孙的同学的名字,语文成绩
select name, chinese from exam_result where chinese>80 and name not like '孙%';
//8.查找孙某同学,否则要求总成绩>200并且语文成绩<数学成绩并且英语成绩>80,
//这些同学的名字,各科成绩和总成绩
select name, chinese, math, english, chinese+math+english as total from esam_result
where (name like '孙_') or (chinese+math+english>200 and chinese<math and english>80);
Ordo per clausulam ponitur pro interrogatione proventuum qualificata. Si ordo clausulae nullus est, ordo eventus indefinitus est.
Sequentia lectio:
asc (ascendendo) --Ascending order
pretium (descending) --descending order
Default est asc
Nota: NULLUS cum aliis valoribus comparari non potest.
//1.查找所有同学的名字和数学成绩,结果按数学成绩升序显示
select name, math from exam_result order by math asc;
//2.查询所有同学的名字和各门成绩,依次按数学降序,英语升序,英语升序显示(若前面条件相等则依次往后比较)
select name, math, chinese, english from exam_result
order by math desc, english asc, chinese asc;
//3.查询所有同学的名字和总分,结果按总分升序显示
select name, math+chinese+english as total from exam_result order by math+chinese+english asc;
//可以这么写吗?可以!!!因为是先把结果select出来,然后再执行order by排序
select name, math+chinese+english as total from exam_result order by total asc;
//4.查询姓孙或姓曹的同学的名字和数学成绩,结果按数学成绩降序显示
select name, math from exam_result where name like '孙%' or name like '曹_' order by math desc;
Ostendere partiales proventus ut requiratur
select * from exam_result limit 3; //从起始行开始,连续显示3行
select * from exam_result limit 2, 4; //从第2行(第0行是起始行),连续显示4行
select * from exam_result limit 4 offset 2; //从第2行(第0行是起始行),连续显示4行
update tb_name set column=expr [, column=expr …] [ubi …] [ordo per …] [limit …]
In universum, renovatio cum clausula adhiberi solet, alioquin omnia viscus renovabitur. Id est, narrantur,Praevia renovationis actio quaesita est
//1.将孙悟空的数学成绩变更为80分
update exam_result set math=80 where name='孙悟空';
//2.将曹孟德的数学成绩改成60,语文成绩改成70
update exam_result set math=60, chinese=70 where name='曹孟德';
//3.将总成绩倒数前三的3位同学的数学成绩加上30分
update exam_result set math=math+30 order by chinese+math+english asc limit 3;
//4.将所有同学的语文成绩更新为原来的两倍
update exam_result set chinese=chinese*2;
dele ab tb_name [ubi ...] [ordo per ...] [terminum ...]
Sicut renovatio, consueta praepositio inquisitionis deletae est
//1.删除孙悟空同学的考试信息
delete from exam_result where name='孙悟空';
//2.删除总分最低同学的考试信息
delete from exam_result order by math+chinese+english asc limit 1;
//清空表(删除所有条目)
delete from exam_result;
truncate exam_result;
Utraque delere et truncata mensam purgare, sed truncatum reddet contra auto_incrementum. (Sunt aliae differentiae, de quibus postea, cum de rebus loquimur) dicetur.
Casus: Deduplicatio tabularum duplicata_table
//第一步:创建一个和duplicate_table结构一样的表no_duplicate_table
create table no_duplicate_table like duplicate_table;
//第二步:将去重查询duplicate_table的结果插入到no_duplicate_table中
insert into no_duplicate_table select distinct * from duplicate_table;
//第三步:将duplicate_table改成其它名字,将no_duplicate_table重命名为duplicate_table
alter table duplicate_table rename to old_duplicate_table;
alter table no_duplicate_table rename to duplicate_table;
Aggregatio sic dicta est plures ordines quaestionis datas in unum ordinem congregare.
officium | illustrare |
---|---|
numerare | Refert numerum data queried |
sum | Refert summam notitiae inquisitionis, quae numerorum tantum significativa est |
avg | Mediocris notitiarum quaesitarum redit, quae numerorum modo significativa est |
max | Mediocris notitiarum quaesitarum redit, quae numerorum modo significativa est |
min | Refert minimum valorem notitiarum quaesitarum, quae numerorum tantum significativa est |
//1.统计班级有多少名同学
select count(*) from exam_result; //相当于统计条目的个数
+----------+
| count(*) |
+----------+
| 7 |
+----------+
//也可以像下面这样写
select count(1) from exam_result;
+----------+
| count(1) |
+----------+
| 7 |
+----------+
//原因在于执行select 1 from exam_result, 效果如下:
+---+
| 1 |
+---+
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
+---+
//2.统计数学成绩有多少个
select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
| 7 |
+-------------+
//3.统计有多少个不同的数学成绩
select count(distinct math) from exam_result;
//4.统计数学成绩的总分
select sum(math) from exam_result;
//5.统计数学成绩平均分
select sum(math)/count(*) from exam_result;
或者
select avg(math) from exam_result;
//6.统计英语成绩不及格的人数
select count(*) from exam_result where english<60;
//7.统计总成绩的平均分
select avg(math+chinese+english) from exam_result;
//可以这样写吗?不能!!!max(math)是math这一列综合计算得出的结果,而每个条目的name都不同,不能压缩成1个
//语法上不允许将它们放一起
//select name, max(math) from exam_result;
select max(math), min(math) form exam_result; //这样写是可以的,
Propositum aggregationis est aggregatum faciliorem mutant.
Circulum per clausulam utere in ad coetum eligendum per columnam determinatam et postea quaesitum.
Format: select column1, column2, ... from table group by column;
Causa:
Praeparatio, efficio mensam molestie informationem (ex oraculo testi classic 9i)
- EMP molestie mensam
- DEPT department mensam
- SALGRADE salarium gradus mensa
//1.显示每个部门的平均工资和最高工资
select deptno, avg(sal), max(sal) from EMP group by deptno;
//deptno相同的条目会分成一组
//这里deptno和聚合函数能放在一起,因为同一组内的deptno是相同的,可以压缩成1个
//2.显示每个部门每种岗位的平均工资和最低工资
select deptno, job, avg(sal), min(sal) group by deptno, job;
//deptno, job相同的条目会分成一组
//3.显示平均工资低于2000的部门和它的平均工资
select deptno, avg(sal) as deptavg from EMP group by deptno having deptavg < 2000;
//说明:having是对聚合后的统计数据,进行条件筛选,通常和group by搭配使用
VS ubi habent
Ambas habentibus et ubi ad eliquationem conditionalem adhiberi possunt, applicatio missionum omnino diversa est;
- ubi percolatur in aliqua specifica columna conditionalem praestat
- habens conditionalem eliquare eventus post adjunctio
Essentia est, quia condiciones protegendi sunt gradus diversi.
Ordo exsecutionis cuiuslibet keyword in SQL quaesitum est:
from > on> join > where > group by > with > having > select > distinct > order by > limit