实现自定义查询的数据库设计及实现

Posted yika的学习乐园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现自定义查询的数据库设计及实现相关的知识,希望对你有一定的参考价值。

上部分大概讲了一下表设计,这部分讲一下处理。

处理的结构

 

处理结构的内容比较多,分为几个部分分别讲解一下。首先讲解一下寻找关系表。

寻找关系表

寻找关系表根据“表间关系登记表”进行处理。假设用户要进行查询时,先从某一个业务表为查询点,则需要把这个业务表作为一个起始点,存储过程根据这个表在“表间关系登记表”递归查找出所有相应的表。这里使用了一个中间表,用来记录递归出来的关系结果;

中间表结构

字段描述

字段名

类型

说明

中间表ID关键字

id_tabtemp

bigint

 

联接标识

cnnid

bigint

 

条件

where_list

varchar(2000)

 

表名

table_name

varchar(2000)

 

表别名

tableas_name

varchar(2000)

 

表中文别名

tableas_cnname

varchar(2000)

 

级次

level_cnt

int

 

关联表ID

id_tabrela

int

 

表ID

id_table

int

 

是否末级

end_flag

char(1)

 

最终表别名

table_asname

varchar(200)

暂不用

上级中间表ID关键字

id_tabtemp_parent

bigint

暂不用

上级表别名

table_asname_parent

varchar(200)

暂不用

主业务表

main_flag

char(1)

 

本级列名

column_name

varchar(200)

 

上级列名

column_name_parent

varchar(200)

 

上级关联表ID

id_tabrela_parent

int

 

from子句

from_list

varchar(2000)

 

 

获取表关系的存储过程(mysql)

  1 create procedure bas_getrelatablepublic_sp
  2 
  3 (
  4 
  5    p_idtable int     -- 表ID
  6 
  7 )
  8 
  9 begin
 10 
 11    declare v_levelcnt int;
 12 
 13    declare v_idparent int;
 14 
 15    declare v_tableasparent varchar(200);
 16 
 17   
 18 
 19   
 20 
 21    set v_levelcnt = 1;
 22 
 23   
 24 
 25    -- 使用表ID作为 cnn ,判断计算过了,就不再计算,减少计算次数
 26 
 27    -- 如果新增关联表,需要根据表ID把此表的数据删除
 28 
 29   
 30 
 31    if not exists ( select 1 from bas_table_temp_tb f where f.cnnid = p_idtable  ) then
 32 
 33       select id_tabrela,tableas_name into v_idparent,v_tableasparent
 34 
 35       from bas_table_relate_tb
 36 
 37       where id_table = p_idtable
 38 
 39             and id_table_rela = p_idtable
 40 
 41             and column_name = column_name_rela ;
 42 
 43         
 44 
 45       insert into bas_table_temp_tb ( cnnid,where_list,
 46 
 47                                       table_name,level_cnt,id_table,tableas_name,end_flag,id_tabrela,main_flag,tableas_cnname,
 48 
 49                                       id_tabtemp_parent,table_asname_parent,column_name,column_name_parent,id_tabrela_parent )
 50 
 51       select distinct p_idtable,concat(" from ",c.tablereg_name," ",b.tableas_name ) whsql,
 52 
 53              c.tablereg_name,v_levelcnt,c.id_table,b.tableas_name,"1",b.id_tabrela,"1",b.column_cnname,
 54 
 55              v_idparent,v_tableasparent,"","",0
 56 
 57       from bas_table_relate_tb b,bas_table_tb c
 58 
 59       where b.id_table = p_idtable
 60 
 61             and b.id_table = b.id_table_rela
 62 
 63             and b.id_table = c.id_table
 64 
 65             and b.column_name = c.prk_name;
 66 
 67      
 68 
 69       set v_tableasparent = concat("A",last_insert_id(),"A");
 70 
 71      
 72 
 73       insert into bas_table_temp_tb ( cnnid,where_list,
 74 
 75                                       table_name,level_cnt,id_table,tableas_name,end_flag,id_tabrela,main_flag,tableas_cnname,
 76 
 77                                       id_tabtemp_parent,table_asname_parent,column_name,column_name_parent,id_tabrela_parent )
 78 
 79       select distinct p_idtable,concat(" left join ",c.tablereg_name," ",b.tableas_name," on ",b.tableas_name,".",b.column_name_rela," = ",h.tableas_name,".",b.column_name ) whsql,
 80 
 81              c.tablereg_name,v_levelcnt,c.id_table,b.tableas_name,"1",b.id_tabrela,"0",concat(b.relatable_name,"[",b.column_cnname,"]"),
 82 
 83              v_idparent,v_tableasparent,b.column_name_rela,b.column_name,v_idparent
 84 
 85       from bas_table_relate_tb b,bas_table_tb c,bas_table_relate_tb h
 86 
 87       where b.id_table = p_idtable
 88 
 89             and b.id_table_rela = c.id_table
 90 
 91             and b.id_table = h.id_table
 92 
 93             and b.id_table = h.id_table_rela
 94 
 95             and h.id_table = h.id_table_rela
 96 
 97             and h.column_name = h.column_name_rela
 98 
 99             and b.id_table <> b.id_table_rela;
100 
101   
102 
103       while row_count() > 0 do
104 
105          insert into bas_table_temp_tb ( cnnid,where_list,
106 
107                                          table_name,level_cnt,id_table,tableas_name,end_flag,id_tabrela,main_flag,tableas_cnname,
108 
109                                          id_tabtemp_parent,column_name,column_name_parent,id_tabrela_parent )
110 
111          select distinct p_idtable,concat(" left join ",c.tablereg_name," ",e.tableas_name,b.tableas_name," on ",e.tableas_name,b.tableas_name,".",b.column_name_rela," = ",e.tableas_name,".",b.column_name ) whsql,
112 
113                 c.tablereg_name,v_levelcnt + 1,c.id_table,concat(e.tableas_name,b.tableas_name),"1",b.id_tabrela,"0",concat(e.tableas_cnname,"|",b.relatable_name,"[",b.column_cnname,"]"),
114 
115                 e.id_tabtemp,b.column_name_rela,b.column_name,e.id_tabrela
116 
117          from bas_table_relate_tb b,bas_table_tb c,bas_table_temp_tb e
118 
119          where e.cnnid = p_idtable
120 
121                and e.id_table = b.id_table
122 
123                and b.id_table_rela = c.id_table
124 
125                and b.id_table <> b.id_table_rela
126 
127                and e.level_cnt = v_levelcnt
128 
129                and not exists ( select 1 from bas_table_temp_tb f where f.cnnid = p_idtable and f.id_table = c.id_table  );
130 
131               
132 
133          set v_levelcnt = v_levelcnt + 1;
134 
135       end while;
136 
137   
138 
139    end if;
140 
141   
142 
143    update bas_table_temp_tb set table_asname = concat("A",id_tabtemp,"A")
144 
145    where cnnid = p_idtable
146 
147          and table_asname is null;
148 
149   
150 
151    update bas_table_temp_tb set table_asname_parent = concat("A",id_tabtemp_parent,"A")
152 
153    where cnnid = p_idtable
154 
155          and table_asname_parent is null;
156 
157         
158 
159    update bas_table_temp_tb set from_list = concat(" left join ",table_name," ",table_asname," on ",table_asname,".",column_name," = ",table_asname_parent,".",column_name_parent)
160 
161    where cnnid = p_idtable
162 
163          and main_flag = "0";
164 
165         
166 
167    update bas_table_temp_tb set from_list = concat(" from ",table_name," ",table_asname)
168 
169    where cnnid = p_idtable
170 
171          and main_flag = "1";    
172 
173 end;

 

以上是关于实现自定义查询的数据库设计及实现的主要内容,如果未能解决你的问题,请参考以下文章

VS Code中自定义Emmet代码片段

会计啥报表需要通过自定义报表实现

VS中添加自定义代码片段——偷懒小技巧

Visual Studio 自定义代码片段在方法定义的参数列表中不起作用

织梦cms如何实现自定义表单字段的查询

什么报表需要通过自定义报表实现的