引用:21. 觸發(fā)程序
create table a (sa int);
create table b (sb int);
delimiter //
create trigger a_bi
before insert on a
for each row Begin
insert into b values(new.sa*100);
end;//
delimiter ;
insert into a values(1);
select * from b ;> 100
Skynet---------- ---------- 我的新 blog : liukaiyi.cublog.cn ---------- ---------- |
引用:http://bbs.mysql.cn/thread-9135-1-2.html
引用:21. 觸發(fā)程序 create table a (sa int); create table b (sb int); drop trigger a_bi ;
delimiter // create trigger a_bi before insert on a for each row Begin insert into b values(new.sa*100); end;// delimiter ; insert into a values(1); select * from b ;> 100 jdbc url: 詳細(xì)屬性可參考 Eg: jdbc:mysql://localhost:3306/test?user =root&password=&useUnicode=true&characterEncoding=utf8 mysql URL: mysql -uroot -p --default-character-set=utf8 這就重點(diǎn)說下:SHOW 1. 查看全部庫支持字符 如:'gb%' -gbk,gb2312 SHOW CHARACTER SET LIKE '%' ; 2. 查看列 show columns from user from mysql ; 3. 查看數(shù)據(jù)庫,表結(jié)構(gòu);當(dāng)然你們也可以仿照下此再建自己的庫,表 show 4.權(quán)限查看 SHOW GRANTS FOR 'root'@'localhost'; .....(詳細(xì)參考 )
這就上寫自己一些有感覺的sql :參考 1.只查詢重復(fù) Eg: create
table
c (id
int
);
insert into c values ( 1 ),( 2 ),( 3 ),( 4 ),( 3 ),( 5 ),( 6 ),( 1 ); 結(jié)果: select id from c group by id having count (id) > 1 ; 2.報(bào)表查詢橫向輸出 Eg: Create table d(id int,name varchar(50)); select
sum ( case when name = ' gly ' then 1 else 0 end ) as gly , sum ( case when name = ' ptgly ' then 1 else 0 end ) as ptgly , sum ( case when name = ' ybgly ' then 1 else 0 end ) as ybgly from d ; 3.復(fù)雜組合查詢 create table table_a (No int, No2 int,num double,itime date); //當(dāng)然也可以 使用mysql 時(shí)間函數(shù) 在軟件編輯時(shí) 你可以輸入 String[] 并根據(jù)數(shù)據(jù)動(dòng)態(tài)拼寫 sql( case部分!! )
//這個(gè) 例子很好 哦!報(bào)表可以一句sql 得出! select NO,NO2, sum ( case when itime like ' 2004-%9% ' then num else 0 end ) as 9M, sum ( case when itime like ' 2004-10% ' then num else 0 end ) as 10M, sum ( case when itime like ' 2004-11% ' then num else 0 end ) as 11M, sum ( case when itime like ' 2004-12% ' then num else 0 end ) as 12M from table_a group by no,no2 order by no,no2 ;
select
tt1.t_Code,tt1.t_name,(
case when exists ( select 1 from tabtest tt2 where tt2.t_code like CONCAT(tt1.t_code, ' % ' ) and tt2.t_code <> tt1.t_code ) then ' you ' else ' wu ' end ) as you_wu from tabtest tt1 ; 就用數(shù)據(jù)數(shù)據(jù)庫表地址數(shù)據(jù)(中國地區(qū)) 來說吧(用Windows 請使用 gbk !!) 可直接運(yùn)行(去除注解) 存儲(chǔ)過程: DELIMITER //
drop procedure if exists findLChild// /* iid 遞歸父節(jié)點(diǎn) , layer 允許遞歸深度 */ CREATE PROCEDURE findLChild(iid bigint(20),layer bigint(20)) BEGIN /*創(chuàng)建接受查詢的臨時(shí)表 */ create temporary table if not exists tmp_table(id bigint(20),name varchar(50)) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*最高允許遞歸數(shù)*/ SET @@max_sp_recursion_depth = 99 ; call iterative(iid,layer);/*核心數(shù)據(jù)收集*/ select * from tmp_table ;/* 展現(xiàn) */ drop temporary table if exists tmp_table ;/*刪除臨時(shí)表*/ END;// DELIMITER ; DELIMITER // drop procedure if exists iterative // CREATE PROCEDURE iterative(iid bigint(20),layer bigint(20)) BEGIN declare tid bigint(20) default -1 ; declare tname varchar(50) character set utf8; /* 游標(biāo)定義 */ declare cur1 CURSOR FOR select id,name from location where fid=iid ; declare CONTINUE HANDLER FOR SQLSTATE '02000' SET tid = null; /* 允許遞歸深度 */ if layer>0 then OPEN cur1 ; FETCH cur1 INTO tid,tname ; WHILE ( tid is not null ) DO /* 核心數(shù)據(jù)收集 */ insert into tmp_table values(tid,tname); call iterative(tid,layer-1); FETCH cur1 INTO tid,tname ; END WHILE; end if; END;// DELIMITER ;
//運(yùn)行!! mysql> call findLChild(1,1);
|