樺仔的博客園
[鏈接]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