Mysql---Sql基本语法二

where和limit

在生产环境中如果查询语句不加限制,可能会拉跨mysql

  1. dbeaver 自动有limit,默认200.
  2. DBA/IT kill (show processlist) 如果这么解决有可能还是不行
  3. JDBC 如果确实要拿1kw数据,不同写法肯定不行,需要用流式写法。

数据准备

create table emp (
    empno numeric(4) not null comment '员工号',
    ename varchar(10) comment '员工姓名',
    job varchar(9) comment '工作',
    mgr numeric(4) comment '上级编号',
    hiredate datetime comment '受雇日期',
    sal numeric(7, 2) comment '薪金',
    comm numeric(7, 2) comment '佣金',
    deptno numeric(2) comment '部门编号'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='员工表';
insert into emp values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);
insert into emp values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
insert into emp values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
insert into emp values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);
insert into emp values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
insert into emp values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);
insert into emp values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);
insert into emp values (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, null, 20);
insert into emp values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);
insert into emp values (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
insert into emp values (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, null, 20);
insert into emp values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);
insert into emp values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);
insert into emp values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);
create table testa(aid int,aname varchar(100),address varchar(100));
create table testb(bid int,bname varchar(100),age int);
create table testc(cid int,sal int);
insert into testa values(1,'x1','sh');
insert into testa values(2,'x2','hz');
insert into testa values(3,'x3',null);
insert into testa values(4,'x4','bj');
insert into testa values(5,'x5','gz');
insert into testb values(1,'x1',10);
insert into testb values(2,'x2',11);
insert into testb values(3,'x3',12);
insert into testb values(4,'x4',16);
insert into testb values(7,'x7',19);
insert into testb values(8,'x8',22);
insert into testb values(9,'x9',24);
insert into testb values(10,'x10',44);

案例演示

1.求员工表所有人的薪水和

select 
sum(sal) as ss
from emp;

2.求员工表的各个部门的薪水和

select 
deptno,
sum(sal) as ss
from emp 
group by deptno;

3.求员工表的各个部门的薪水和、员工数、平均薪资

select 
deptno,
sum(sal) as ssum,
count(ename) as pcount,
sum(sal)/count(ename) as meansal1,
avg(sal) as meansal2
from emp 
group by deptno;

4.找薪水和>9000的是哪个部门?

select 
deptno,
sum(sal) as ssal
from emp
group by deptno
having ssal>9000;

5.子查询

select 
*
from
(select 
deptno,
sum(sal) as ssal
from emp
group by deptno) as t where t.ssal>9000;

语法总结:
1.聚合函数 sum count avg max min
2.分组语法 select xxx,sum(yyy) from t group by xxx
3.group by出现的字段 务必出现在 select 后面
4.注意区分sum count
5.having 过滤 等价于 子表+where
综合总结:
select -> from -> where -> group by -> order by -> limit


6.left join语法 (以左表为主 a<--b a数据最全 b是匹配 匹配多少算多少 on就是匹配条件)

select 
a.*,
b.*
from testa as a 
left join testb as b  on a.aid=b.bid

7.right join 以右表为主 a-->b b数据最全 a是匹配 匹配多少算多少 on就是匹配条件

select 
a.*,
b.*
from testa as a 
right join testb as b  on a.aid=b.bid

8.inner join 两表的交集 on就是匹配条件

select 
a.*,
b.*
from testa as a 
inner join testb as b  on a.aid=b.bid;

9.full join mysql不支持,用左连接 +union + 右连接

select 
a.*,
b.*
from testa as a 
left join testb as b  on a.aid=b.bid
union
select 
a.*,
b.*
from testa as a 
right join testb as b  on a.aid=b.bid

10.union all 结果不去重复 union去重复 注意:数量相同 类型相同

select  aid from testa
union all 
select  bid from testb;

select  aid from testa
union  
select  bid from testb;

select  bid as id from testb
union  
select  aid from testa;

11.分组求topN,哪些部门的哪些职业的薪水和,最高1位的职业是什么?

drop view sal;
create view  sal
as 
select
deptno,job,
sum(sal+ifnull(comm,0)) as sal
from  emp 
group by  deptno,job;

select * from sal;

select 
a.*
from sal a 
where
(
select count(*) from sal b 
where a.deptno=b.deptno
and a.sal<b.sal
) =0 
order by a.deptno;
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341

推荐阅读更多精彩内容