mysql 之 增删改查(二)2017-08-04

桦仔的博客园
[链接]http://www.cnblogs.com/lyhabc/category/573945.html

1.去除结果集中重复数据

select distinct sex, age from student;

2.模糊查询:全模糊=关键字搜索

select * from student where sname like '%王';

3.模糊查询:左模糊=右边是固定的,左边是任意的

select * from student where sname like '%二';

存储过程:有参数的方法(主)

// 输入参数(传入实参给方法内部使用的):key,value
// 输出参数(将方法内部的结果带出来的):map
public void test (int key,String value,HashMap map){
    map.put(key,value);
}
HashMap map = new HashMap();
test(1,"holly",map);
create procedure p

1.1 定义无参数的存储过程

//定义语句结束用//

delimiter //
create procedure proc_teacher_noparam()
begin
   select * from teacher;
end
//

//定义语句结束用;

delimiter ;

1.2.调用无参数的存储过程

call proc_teacher_noparam();

2.1 定义输入参数的存储过程

//定义语句结束用//

delimiter //
create procedure proc_teacher_inparam(in n int)
begin
   select * from teacher where id=n;
end
//

//定义语句结束用;
delimiter ;

2.2.调用输入参数的存储过程

//定义变量

set @n=1;

//调用存储过程

call proc_teacher_inparam(@n);

3.1 定义有输出参数的存储过程

drop procedure if exists proc_teacher_outparam;
delimiter //
create procedure proc_teacher_outparam(out n int)
begin
   select count(*) into n from teacher;
end
//
delimiter ;

//3.2 调用输出参数的存储过程

set @n=1;
call proc_teacher_outparam(@n);
select @n;

//4.定义有输入和输出参数的存储过程

delimiter //
drop procedure if exists proc_teacher_in_outparam;
create procedure proc_teacher_in_outparam(in n int,out o int)
begin
   select count(*) into o from teacher where id=n;
end
//
delimiter ;

//4.2 调用带有输入和输出参数的存储过程

sex @n=1;
call proc_teacher_inoutparam(1,@n);
select @n;

//5.1 创建输入输出参数是同一个变量的存储过程

delimiter //
drop procedure if exists proc_teacher_inoutparam;
create procedure proc_teacher_inoutparam(inout n int)
begin
   select count(*) into n from teacher where id=n;
end
//
delimiter ;

//5.2 调用输入输出参数是同一个变量的存储过程

sex @n=1;
call proc_teacher_inoutparam(@n);
select @n;

15.mysql 函数和java 有返回值方法

public int findAgeByCount(int age){
    int count = select count(age)...
    return count;
}

//定义函数
create function fundName2()
returns varchar(20)
return(select sname from student where sid=1);

//调用
select fundName2()

创建视图

create or replace 
view view_student 
as 
select * from student where sid>3;

查看视图内容

select * from view_student;

添加数据(原来有2条)

insert into classes(cname) values('TB111'),('TB50'),('TB47');

提交事物(有5条)

commit;

查询语句(有5条)

select * from classes;

事物回滚(回滚到上一次 commit 之前的操作,2结果)

rollback;

查询语句(有2条)

select * from classes;

内连接(student:5,classes:5)

select * from 
student s
inner join classes c on s.cid=c.cid;

左连接(student为主和 classes 匹配,匹配到返回,匹配到不填充 null)

select * from 
student s
right join class c on s.cid=c.cid;

笛卡尔积查询(table1*table)

select * from 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值