Skynet

---------- ---------- 我的新 blog : liukaiyi.cublog.cn ---------- ----------

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  112 Posts :: 1 Stories :: 49 Comments :: 0 Trackbacks

#

引用: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
posted @ 2008-10-01 18:13 刘凯毅 阅读(1546) | 评论 (0)编辑 收藏

在线文档参考

jdbc url: 详细属性可参考

Eg: jdbc:mysql://localhost:3306/test?user =root&password=&useUnicode=true&characterEncoding=utf8

mysql URL: mysql -uroot -p --default-character-set=utf8


协调 SQL 详细:

  这就重点说下:SHOW

  1. 查看全部库支持字符 如:'gb%'  -gbk,gb2312

      SHOW CHARACTER SET LIKE '%' ;

  2. 查看列

  show columns from user from mysql ;
  show columns from mysql.user ;
  desc mysql.user ;
  //简化
  //更方便的 查看 列名 模糊查询列名为 'Select%' (desc  information_schema.columns 还有更多惊喜)

  select column_name from information_schema.columns where column_name like 'Select%' ;

  3. 查看数据库,表结构;当然你们也可以仿照下此再建自己的库,表

   show
   show create database information_schema"G
   show create table mysql.user"G

  4.权限查看

   SHOW GRANTS FOR 'root'@'localhost';

  .....(详细参考


SQL 详细

这就上写自己一些有感觉的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));
insert into d values(1,'gly');
insert into d values(2,'ptgly');
insert into d values(3,'ybgly');
insert into d values(4,'ptgly');
insert into d values(5,'ybgly');
+---+-----+------+
|gly |ptgly|ybgly|     
+---+-----+------+-
 |1   |2     |2      |
+---+-----+------+

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);
insert into table_a values
     (1234,567890,33.5,'2004-12-21'),
     (1234,598701,44.8,'2004-11-21'),
     (1234,598701,45.2,'2004-10-01'),
     (1234,567890,66.5,'2004-9-21'),
     (3456,789065,22.5,'2004-10-01'),
     (3456,789065,77.5,'2004-10-27'),
     (3456,678901,48.5,'2004-12-21');
按月统计销售表中货物的销售量数
查询结果如下:
  No, No2 ,   九月,  十月,十一月,十二月
1234,567890, 66.5 ,  0 ,  0  ,  33.5
1234,598701,  0   , 45.2, 44.8, 0
3456,789065, 0  ,  100,  0  ,  0
3456,678901, 0 ,    0,    0  ,  48.5
-----------------------------------------------------------------------

//当然也可以 使用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 ;


4.字符集子层关系
1     a
2     b
11    c
(代码11表示为1的下级)
我要通过一条句子,得出如下结果:
Create table TabTest(t_Code varchar(10),t_Name varchar(10));
insert into TabTest values('1','a');
insert into TabTest values('2','b');
insert into TabTest values('11','c');
--------------------------------------------------------------

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 ;
posted @ 2008-10-01 17:22 刘凯毅 阅读(2314) | 评论 (0)编辑 收藏

就用数据数据库表地址数据(中国地区) 来说吧(用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(20default -1 ;
         
declare tname varchar(50character 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);
+------+------------------+
| id   | name             |
+------+------------------+
|    2 | 北京             |
|    4 | 上海             |
|    6 | 香港特别行政区   |
|    8 | 澳门特别行政区   |
|   10 | 河北             |
|   23 | 山西             |
|   35 | 辽宁             |
|   50 | 吉林             |
|   60 | 黑龙江           |
|   74 | 江苏             |
|   88 | 浙江             |
|  101 | 安徽             |
|  119 | 福建             |
|  129 | 江西             |
|  142 | 山东             |
|  160 | 河南             |
|  179 | 湖北             |
|  198 | 湖南             |
|  213 | 广东             |
|  235 | 甘肃             |
|  250 | 四川             |
|  272 | 贵州             |
|  282 | 海南             |
|  301 | 云南             |
|  318 | 青海             |
|  327 | 陕西             |
|  348 | 广西壮族自治区   |
|  363 | 西藏自治区       |
|  371 | 宁夏回族自治区   |
|  377 | 新疆维吾尔自治区 |
|  400 | 内蒙古自治区     |
|  413 | 台湾省           |
+------+------------------+
32 rows in set (0.02 sec)




posted @ 2008-10-01 17:19 刘凯毅 阅读(4042) | 评论 (0)编辑 收藏

仅列出标题
共12页: First 上一页 4 5 6 7 8 9 10 11 12