引用:21. 觸發程序
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. 觸發程序 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: 詳細屬性可參考 Eg: jdbc:mysql://localhost:3306/test?user =root&password=&useUnicode=true&characterEncoding=utf8 mysql URL: mysql -uroot -p --default-character-set=utf8 這就重點說下:SHOW 1. 查看全部庫支持字符 如:'gb%' -gbk,gb2312 SHOW CHARACTER SET LIKE '%' ; 2. 查看列 show columns from user from mysql ; 3. 查看數據庫,表結構;當然你們也可以仿照下此再建自己的庫,表 show 4.權限查看 SHOW GRANTS FOR 'root'@'localhost'; .....(詳細參考 )
這就上寫自己一些有感覺的sql :參考 1.只查詢重復 Eg: create
table
c (id
int
);
insert into c values ( 1 ),( 2 ),( 3 ),( 4 ),( 3 ),( 5 ),( 6 ),( 1 ); 結果: select id from c group by id having count (id) > 1 ; 2.報表查詢橫向輸出 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.復雜組合查詢 create table table_a (No int, No2 int,num double,itime date); //當然也可以 使用mysql 時間函數 在軟件編輯時 你可以輸入 String[] 并根據數據動態拼寫 sql( case部分!! )
//這個 例子很好 哦!報表可以一句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 ; 就用數據數據庫表地址數據(中國地區) 來說吧(用Windows 請使用 gbk !!) 可直接運行(去除注解) 存儲過程: DELIMITER //
drop procedure if exists findLChild// /* iid 遞歸父節點 , layer 允許遞歸深度 */ CREATE PROCEDURE findLChild(iid bigint(20),layer bigint(20)) BEGIN /*創建接受查詢的臨時表 */ create temporary table if not exists tmp_table(id bigint(20),name varchar(50)) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*最高允許遞歸數*/ SET @@max_sp_recursion_depth = 99 ; call iterative(iid,layer);/*核心數據收集*/ select * from tmp_table ;/* 展現 */ drop temporary table if exists tmp_table ;/*刪除臨時表*/ 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; /* 游標定義 */ 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 /* 核心數據收集 */ insert into tmp_table values(tid,tname); call iterative(tid,layer-1); FETCH cur1 INTO tid,tname ; END WHILE; end if; END;// DELIMITER ;
//運行!! mysql> call findLChild(1,1);
|