oracle 多表的查询

oracle 多表的查询,第1张

弄个字符串变量,设个游标,用括号里那一串;

然后遍历表名,每找到一个表名,就在字符串变量里拼上一段:=

'select

from

'||table_name||';',

然后用EXECUTE

IMMEDIATE执行;

然后读下一个表名,直至遍历完毕。

大致就是这么个意思。

oracle同时查询多张表较为简单,用sql和视图都可以完成,但是oracle查询多张视图就不好做了,要是还有分页和检索的功能就更加蛋疼了,今天老夫遇到了这样的问题,接下来就和大伙分享下:

//分页的语句

String fenyeSql_1 = "SELECT FROM ( SELECT A, ROWNUM RN FROM (";

String fenyeSql_2 = " ) A WHERE ROWNUM <= " + end + " ) WHERE RN >= " + from + " ";

//查询条数的语句

String fenyeCountSql_1 = " select count() from ( ";

String fenyeCountSql_2 = " ) ";

String union = " union ";

//第一张视图

String sql_gjy_select = " select to_char(txm) xm,to_char(txb) xb,tqslc qslc,tzzlc ,to_char(t检查日期) 检查日期,to_char(t上传时间) 上传时间,t上传单位,t路局名,tjcsbid sclx from v$gwsj_jcsjlx t where 1=1 ";

//第二张视图

String sql_tqi_select = " select t2xm xm,t2xb xb,t2qslc qslc,t2zzlc,to_char(t2检查日期,'yyyy-MM-dd') 检查日期,to_char(t2上传时间,'yyyy-MM-dd') 上传时间,t2上传单位,t2路局名,4 sclx from v$tqi_line t2 where 1=1 ";

//第三张视图

String sql_g4d_select = " select t3xm xm,t3xb xb,t3qslc qslc,t3zzlc,t3检查日期 检查日期,to_char(t3上传时间,'yyyy-MM-dd') 上传时间,t3上传单位,t3路局名,5 sclx from v$g4d_line t3 where 1=1 ";

//注意三张视图的数据类型要转换一致,而且列的数量要一样哦

//接下来就是蛋疼的凭借sql

// 条件1

if (null != year && null != month && !""equals(year)

&& !""equals(month)) {

String selectDate = year + "-";

if (monthtoCharArray()length == 1) {

selectDate += "0" + month;

} else if (monthtoCharArray()length == 2) {

selectDate += month;

}

sql_gjy_select += " and t检查日期 like '%" + selectDate + "%'";

sql_tqi_select += " and t2检查日期 like '%" + selectDate + "%'";

sql_g4d_select += " and t3检查日期 like '%" + selectDate + "%'";

}

//条件2

if (null != fdwbh && !""equals(fdwbh)) {

sql_gjy_select += " and t上传单位='" + fdwbh + "'";

sql_tqi_select += " and t2上传单位='" + fdwbh + "'";

sql_g4d_select += " and t3上传单位='" + fdwbh + "'";

}

//

//

中间条件省略若干的

//

//

// 查询数据

String sql_select = fenyeSql_1 + sql_gjy_select + union

+ sql_tqi_select + union + sql_g4d_select + fenyeSql_2;

// 对应总条数

String sql_count = fenyeCountSql_1 + sql_gjy_select + union

+ sql_tqi_select + union + sql_g4d_select + fenyeCountSql_2;

PreparedStatement ps = null;

String permissionJson = null;

查询,首先你得清楚你需要什么数据,以及你所需要的数据,存在于哪些表中,或者说与哪些表有关联。

SQL 语句基本查询语法: 不加条件查询,select from 表名 ; 加条件查询:select from 表名 where 满足的条件;

多表查询 :select a ,b from 表1 a,表2 b where a字段=b字段;

用union,举例有S1表(a,b,c,d)和S2表(a,c,d,e)和S3表(f,g),里头的字段不同,但在逻辑上有关系

(如有

s1b=s2e

s1a=s3f

s1b=s3g)

示例如下:

------------------------------------------------------------------------------

select

S1a

as

x,S1b

as

y,S1c

as

z

from

S1

union

select

S2a

as

x,S2e

as

y,S2c

as

z

from

S2

union

select

S3f

as

x,S3g

as

y,''

as

z

from

S3

------------------------------------------------------------------------------

最终结果会是三张表的和,如果S1有10条记录,S2有3条记录,S3有4条记录,则执行本SQL后会得到17条记录,其中来自S3表的数据,第三列一定为空的。

以上就是关于oracle 多表的查询全部的内容,包括:oracle 多表的查询、oracle数据库三张表字段一模一样,怎么多表查询啊、oracle 多表查询 抽样等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9266223.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存