mysql 笔记

Posted TheStar

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 笔记相关的知识,希望对你有一定的参考价值。

select * from order_user;

条件语句case
case
when age<=10 then ‘儿童‘
when age>10 and age <=18 then ‘儿童‘
when age<=10 then ‘儿童‘
when age<=10 then ‘儿童‘
else ‘老年‘
end as user_type


select order_id,
case order_status
when 0 then ‘已下单‘
when 1 then ‘已发货‘
when 2 then ‘已签收‘
else ‘邮件丢失‘
end as order_s,
user_id_order
from order_user;

存储例程 存储过程 函数

create procedure pro_sumnum(in a int,in b int,out numsum int)
begin
select a+b into numsum;
end

存放在内存中,直接调用@numsum使用得到变量

call pro_sumnum(50,15,@numsum);
select @numsum;

set @a = 10;
set @b = 12;

 

create procedure pro_sele(in ids varchar(255))
begin
declare loc int(10);
declare id varchar(11);
ks:loop
set loc = locate(‘,‘,ids); -- 按照字符串定位他的下标
set id = substr(ids,1,loc-1); -- 截取中间的字符串
if loc <> 0 then
set ids = substr(ids,loc+1); -- 截取后面的字符串
else set id = ids;
end if;
select id as ‘输出的结果‘; -- 输出的结果
if loc=0 then
leave ks; -- 截取完后跳出循环
end if;

end loop;
end;

set @str = ‘10,20,43,23,32‘;
call pro_sele(@str);

call pro_sele(‘10,20,43,23,32‘);

循环生成随机数 rand() 0和1

drop procedure if exists pro_rd;
create procedure pro_rd(in c int)
begin
declare temp int;
repeat
select RAND() into temp;
select temp as ‘随机数‘;
set c = c-1;
until c=0
end repeat;
end;

call pro_rd(6);

创建函数
create function fun_sum(num1 int,num2 int) returns int
begin
declare sum int;
set sum = num1+num2;
return sum;
end;

select fun_sum(10,12);


create table history(
id int not null primary key auto_increment,
remark varchar(255)
)charset=utf8,engine=innodb;
触发器:向日志表中运用
create trigger tri_add before insert on goods_table2 for each row
begin
if dayofweek(NOW()) not in(2,3,4,5,6) then
insert into history(remark) values(‘周末更新的数据‘);
else insert into history(remark) values(‘数据更新!!!1‘);
end if;
end

insert into goods_table2 values(9,‘华为手机‘,1999,99,‘files/img/phone.jpg‘,NOW());
insert into goods_table2 values(6,‘水杯‘,424.54,‘files/img/shui.jpg‘,NOW());

insert into goods_table2(goods_name,goods_price,goods_pic,goods_endtime)
values(‘海尔冰箱‘,3999.99,‘files/img/hebx.jpg‘,NOW());
select * from goods_table2;
select * from history;

查询不同用户 订单的数量,订单的总金额 分组用户
筛选出订单数>条, 订单总金额>3999.99

select user_id ,
count(order_id) as o_num,
sum(order_price) as o_price
from order_table where user_id!=3
group by user_id
having o_num>1 and o_price>3999.99
;

having 用于聚合函数后,分组,筛选

 

--常用的函数
abs(值) 绝对值;
rand() 随机数
round() 四舍五入
max() greatest(x1,x2,x3....) 返回集合中最大的值;
min() least(x1,x2,x3....)返回集合中最小的值;
sum()
count()
select * from goods_table;
update goods_table set goods_price=-500.99 where goods_id=3;
select goods_price from goods_table where goods_id=3;
select ABS(goods_price) from goods_table; 绝对值
select ABS(ROUND(goods_price)) from goods_table; 四舍五入
select SUM(goods_price) from goods_table; 求和

select COUNT(goods_unit) from goods_table; 相应字段的条数
select greatest(3999.99,9999.99,-35399.99,353,5326) as max_money; 查询出最大值

-- select greatest(goods_price) as max_price from goods_table; 错误语法
select MAX(goods_price) as max_price from goods_table;

 

--时间函数
time(字段/值) year(字段/值) timestamp(时间) 得到完整的时间
from_unixtime(ts,fmf); 根据指定fmf的格式 格式化时间戳
UNIX_TIMESTAMP(‘时间字符串‘); 转换为时间戳
str_to_date(‘时间字符串‘,format) 将字符串抓换为时间
date_format(NOW(),‘%Y-%m-%d %H:%m:%s‘); 将时间转换为字符串


select * from goods_table;
select time(goods_begintime) as b_time from goods_table;
select year(goods_begintime) as b_year from goods_table group by b_year;
select timestamp(goods_begintime) as n_time from goods_table;

select unix_timestamp(goods_begintime) from goods_table;
select from_unixtime(‘1505098993‘,‘%Y-%i-%d‘)as un_time from goods_table;
select date_format(NOW(),‘%Y-%m-%d %H:%i:%s‘)as g_t,t1.*from goods_table t1;
select str_to_date(‘2017-09-09 11:03:13‘,‘%Y-%m-%d %H:%i‘) as s_t from goods_table;

--字符串
字符串的连接 concat(str1,str2,...);
连接字符串 给定的格式 concat_ws(fmt,str1,str2,...);
返回字符串中指定的字符: (左/右边) left(str,len); right(str,len);

select concat(‘我要‘,‘吃‘,‘火锅‘)as say_info;
select * from goods_table;
select concat(goods_name,goods_price,goods_address) as goods_info from goods_table;
select concat_ws(‘--->‘,goods_name,goods_price,goods_address)as goods_info from goods_table;
select left(concat_ws(‘--->‘,goods_name,goods_price,goods_address),20)as goods_info from goods_table;

sql语句常用的操作
别名: select t1.goods_name as g_name,t1.goods_price as g_price from goods_table t1;

数据排序 order by desc 降序 asc 升序
select * from goods_table order by goods_begintime desc,goods_id asc;

分组 group by SUM(expr) COUNT(expr)
select SUM(goods_price) as gd_price, goods_address,
COUNT(goods_id) as count_num
from goods_table group by goods_address;

mysql的事务
原子性(ACID Atomicity):保证工作单元内的所有操作都成功完成,否则事务将终止在故障点,和以前的操作回滚到以前的状态
一致性(稳定性 Consisteny) 确保正确的改变状态后,成功的提交事务
隔离性(Isolation):使事务操作彼此独立和透明
持久性(Durability):使事务提交后的数据,在出现故障或者 系统崩溃的情况下数据仍然存在,保证数据的正确性

mysql中事务的操作步骤:

1 找到mysql 的安装路径
2 通过mysql.exe执行命令 mysql -u用户名 -p密码 -h主机地址
3 开启事务 start transaction
4 事务中需要执行的内容...........
5 提交事务 持久性 commit
6 保存点/折返点 savepoint 折返点名称
7 回到折返点: rollback to savepoint 折返点名称

注意:MyISAM: 不支持事务,用于只读程序中 提高性能 --存储独立于操作系统
Innodb: 支持ACID事务,行级锁,并发 --事务型存储引擎
Berkeley db: 支持事务

 



































































































































以上是关于mysql 笔记的主要内容,如果未能解决你的问题,请参考以下文章

MySQL学习笔记五

MySQL学习笔记——存储过程

2016-2-21mysql笔记

MySQL学习笔记六

mysql笔记

MySQL学习笔记四