Mysql高级第一天(laojia)
select char_length('我们love');
select *, char_length(sname) '姓名字数' from tbl_student;
select '对方' + '5';
select '学号:S10013,姓名:'
select CONCAT('学号:', sno, ',姓名:', sname) '学生西宁' from tbl_student;
select insert('hello,jzp,hi',1, 0, 'dd');
select REPLACE('我们的AAA主席非常厉害,我爱AAA。', 'AAA', '****')
select left('zhangwuji@qq.com', locate('@', 'zhangwuji@qq.com')-1) 'username';
select right('zhangwuji@qq.com', char_length('zhangwuji@qq.com') - locate('@', 'zhangwuji@qq.com') );
select SUBSTRING('zhangwuji@qq.com', locate('@', 'zhangwuji@qq.com')+1);
select *, ifnull(saddr, '未知住址') as address, if(sex = '男', 'male', 'female') as 'gender' from tbl_student;
select
sname,
sscore,
sdengji,
case
when sscore >= 90 then 'Good'
when sscore >= 60 then 'Normal'
else 'Bad'
end as en,
case sdengji
when '优' then '☆☆☆'
when '中' then '☆☆'
else '☆'
end as 'star',
saddr
from tbl_student;
select now();
select left(current_date(),4);
select current_time();
-- 获取当前日期时间(指定日期时间)的时间戳
select unix_timestamp();
select year(now());
select month(now());
select day(now()), dayofmonth(now());
select hour(now());
select minute(now());
select second(now());
select dayofweek('1995-5-3'), weekday('1995-5-3'), dayname(now());
select DAYOFYEAR(now());
select week(now()), WEEKOFYEAR(now());
select * from where year(sellTime) = year(now()) and month(now()) - month(sellTime) = 1
select TIMESTAMPDIFF(year, '2016-8-25 8:27:00', now());
select DATE_ADD(now(),INTERVAL (-5) minute)
drop database if exists test;
create database test;
use test;
create table tbl_klass
(
kid int primary key auto_increment,
kname varchar(20) not null
);
create table tbl_student
(
sid int primary key auto_increment,
sno char(6) not null unique,
sname varchar(20) not null,
sbirthdate date,
sex enum('男', '女') not null,
saddr varchar(100) default '博为峰学生公寓',
sregistTime datetime not null,
skid int not null,
foreign key (skid) references tbl_klass (kid)
);
insert into tbl_klass (kname) values ('11期'), ('12期'), ('13期');
select * from tbl_klass;
insert into tbl_student values (null, 'S10015', '杨玉松', '1970-9-2', '女', default, now(), 1);
select * from tbl_student;
/* 创建一个视图 */
create view v_students as
select sno, sname, kname from tbl_student left join tbl_klass on skid = kid;
/* 删除一个视图 */
drop view if exists v_students;
-- 删除触发器
drop trigger if exists trig_tx;
-- 创建触发器
-- trig_tx 会监听tbl_trans表,当在tbl_trans表上发生了一次插入后
-- 执行触发器的主体内容
create trigger trig_tx before insert on tbl_trans for each row
BEGIN
declare v decimal(20, 2);
declare b decimal(20, 2);
select abalance into b from tbl_acount where anum = new.tnum;
if new.ttype = '收入' or new.tmoney <= b then
if new.ttype = '支出' then
set v = 0 - new.tmoney;
else
set v = new.tmoney;
end if;
update tbl_acount set abalance = abalance + v where new.tnum = anum;
ELSE
-- 用户抛出自定义异常
signal SQLSTATE 'HY000' set message_text = '交易失败:余额不足!';
end if;
END;
-- 删除触发器
drop trigger if exists trig_tx2;
create trigger trig_tx2 after update on tbl_trans for each ROW
BEGIN
if old.ttype = '支出' then
update tbl_acount set abalance = abalance + old.tmoney - new.tmoney where anum = old.tnum;
ELSE
update tbl_acount set abalance = abalance - old.tmoney + new.tmoney where anum = old.tnum;
end if;
end;
drop PROCEDURE if exists stu_regist;
create procedure stu_regist(out total int, no char(6), name varchar(20), birthdate date, se varchar(10), addr varchar(100), klassName varchar(20))
BEGIN
declare klassId int;
select kid into klassId from tbl_klass where kname = klassName;
if addr = '' then
insert into tbl_student (sno, sname, sbirthdate, sex, saddr, skid, sregistTime) values (no, name, birthdate, se, default, klassId, now());
else
insert into tbl_student (sno, sname, sbirthdate, sex, saddr, skid, sregistTime) values (no, name, birthdate, se, addr, klassId, now());
end if;
update tbl_klass set kstucount = kstucount + 1 where kid = klassId;
select kstucount into total from tbl_klass where kid = klassId;
end;
set @t = -1;
call stu_regist(@t, 'S10047', '珠儿3', '1995-5-5', '女', 'fff', '13期');
select @t;
call proc_testWhile();
drop procedure if exists proc_testLoop;
create procedure proc_testLoop()
BEGIN
declare i int;
set i = 1;
lp1 : LOOP
if i = 4 then
set i = i + 1;
iterate lp1;
end if;
select i;
set i = i + 1;
if i > 10 then
leave lp1;
end if;
end loop lp1;
end;
insert into tbl_acount values ('6223 1234 4567 8888', 1000, now());
insert into tbl_acount values ('6223 1234 4567 9999', 500, now());
insert into tbl_acount values ('6223 1234 4567 0000', 567500, now());
insert into tbl_acount values ('6223 1234 4567 1111', 10, now());
insert into tbl_acount values ('6223 1234 4567 2222', 3200, now());
insert into tbl_trans values (null, '6223 1234 4567 8888', '收入', 1500, now());
insert into tbl_trans values (null, '6223 1234 4567 8888', '支出', 500, now());
drop procedure if exists payAll;
create procedure payAll(m decimal(20, 2))
BEGIN
declare totalCount int;
declare i int;
declare num varchar(19);
declare ban decimal(20, 2);
declare tt datetime;
-- 跟新所有账户的余额
update tbl_acount set abalance = abalance + m;
-- 生成所有的交易记录(金额:m,类型:收入,时间:now())
select count(*) into totalCount from tbl_acount;
set i = 0;
set tt = now();
while i < totalCount DO
select anum into num, abalance into ban from tbl_acount limit i, 1;
if ban >= 1000 then
insert into tbl_trans values (null, num, '收入', m, tt);
end if;
set i = i + 1;
end while;
end;
call payAll(1);
select * from tbl_acount;
select * from tbl_trans;
drop PROCEDURE if exists payAll2;
create procedure payAll2( m decimal(20,2) )
begin
declare num varchar(19);
declare tt datetime default now();
declare over int default 0; -- 定义了一个普通int类型变量over,初始值0;用over来指示游标是否已经读完
declare c cursor for select anum from tbl_acount; -- 定义(声明)一个游标
declare continue handler for not found set over = 1; -- 条件定义(当发现某个游标,某次fetch后,没有found到数据,则触发这个条件,将变量over设置为1)
open c; -- fetch之前先要打开游标
lp: LOOP
fetch c into num; -- 获取游标遍历的结果集当前指针的下一行数据,并且抓取到给变量num赋值。也可能这一次(下一行没数据)而没有found到数据
if over = 1 THEN
leave lp;
end if;
insert into tbl_trans values (null, num, '收入', m, tt);
end loop lp;
close c; -- 用完游标后,要关闭游标
end;
各科成绩前两名:SELECT * FROM tbl_exam a1 WHERE (SELECT count(*) FROM tbl_exam a2 where a1.ecouse=a2.ecouse and a1.escore<a2.escore)<2 ORDER BY ecouse,escore DESC
Mysql高级第一天(laojia)的更多相关文章
- MySQL高级第一章——架构介绍
一.简介 是一个经典的RDBMS,目前归属于Oracle 高级MySQL包含的内容: MySQL内核 SQL优化工程师 MySQL服务器的优化 各种参数常量设定 查询语句优化 主从复制 软硬件升级 容 ...
- mysql笔记04 MySQL高级特性
MySQL高级特性 1. 分区表:分区表是一种粗粒度的.简易的索引策略,适用于大数据量的过滤场景.最适合的场景是,在没有合适的索引时,对几个分区进行全表扫描,或者是只有一个分区和索引是热点,而且这个分 ...
- 第三章 MySQL高级查询(一)
第三章 MySQL高级查询(一) 一.SQL语言的四个分类 1. DML(Data Manipulation Language)(数据操作语言):用来插入,修改和删除表中的数据,如INSE ...
- MySQL高级知识(十四)——行锁
前言:前面学习了表锁的相关知识,本篇主要介绍行锁的相关知识.行锁偏向InnoDB存储引擎,开销大,加锁慢,会出现死锁,锁定粒度小,发生锁冲突的概率低,但并发度高. 0.准备 #1.创建相关测试表tb_ ...
- MySQL高级知识(六)——索引优化
前言:索引优化的目的主要是让索引不失效,本篇通过相关案例对索引优化进行讲解. 0.准备 创建经典的tb_emp表. DROP TABLE IF EXISTS `tb_emp`; CREATE TABL ...
- mysql高级、索引
一.mysql高级 1.视图 # 引子 select * from emp left join dep on emp.dep_id = dep.id union select * from emp r ...
- Mysql 高级部分
MySQL 高级部分 (1)索引(index)..................................................................... 1 (2) ...
- 小白两篇博客熟练操作MySQL 之 第一篇
小白两篇博客熟悉操作MySQL 之 第一篇 一.概述 1. 什么是数据库? 答: 储存数据的仓库, 如: 在ATM的事例中创建的一个db 目录, 称为数据库 2. 什么是Mysql, Oracl ...
- 干货:鲜为人用的MySQL高级特性与玩法!
上一篇文章<万字总结:学习MySQL优化原理,这一篇就够了!>文末给大家留有两个开放的问题: 有非常多的程序员在分享时都会抛出这样一个观点:尽可能不要使用存储过程,存储过程非常不容易维护, ...
随机推荐
- PHP的类,abstract类,interface及关键字extends和implements
原文:https://blog.csdn.net/qq_19557947/article/details/77880757?locationNum=4&fps=1 PHP类 PHP类是单继承, ...
- sap QG3搜索
先创建一个QG3系统,创建一个用户. 1: 进入搜索模板 2: 选择软件组件,点击执行 3: 设置过滤条件. 4: 选择在哪一列 设置过滤条件. 5: 定义搜索值 6: 设置值 可以将搜索的结果删除. ...
- App导航设计全面梳理——附免费原型模版!
生活中大家或多或少都会有迷路的经验,但你是不是从来没思考过迷路的定义是什么? 迷路的定义其实有两个核心: 1.想要到达一个目的地. 2.不知道自己在哪里,应该往哪走. 和生活中的迷路一样,我们在使用A ...
- SDWebImage代码赏析
过NSOperationQueue来控制 为了保护变量,使用大量了sychronized 读的时候使用了dispatch_sync dispatch_sync(sself.barrierQueue, ...
- 关于微信分享的一些心得之recommend.js(直接复制就行)
// import $ from 'jquery'import Vue from 'vue'export default function (type,title,con,img,url,) { / ...
- 强势龙头股的 VOLM5/35/135 走势,可作为逃顶参考
强势龙头股的 VOLM5/35/135 走势,可作为逃顶参考
- 《2017年Q2中国城市研究报告
根据百度慧眼团队发布的<2017年Q2中国城市研究报告>,2017年第二季度人口吸引力排名前五的城市与第一季度相同,深圳继续保持第一的领先位置.
- PHP与JSP简单比较
比较PHP和JSP这两个Web开发技术,在目前的情况是其实是比较php和Java的Web开发.以下就几个主要方面进行的比较: 一. 语言比较 PHP是解释执行的服务器脚本语言,首先php有简单容易上手 ...
- struts2.0自定义类型转换
在Struts2.0框架中内置了类型转换器,可以很方便的实现在八大数据类型.Date类型之间的自动转换:此外也可以根据自己的需求自定义数据转换类.如下: 首先看一下项目工程中的目录 1.在新建的web ...
- git 开发中的总结
一.git是什么 1.git是一种分布式的版本管理系统, 分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库. 二.g ...