桦仔的博客园
[链接]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